0
0
PowerShellscripting~10 mins

Parameter validation in PowerShell - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to validate that the parameter $Age is an integer.

PowerShell
param([[1]]$Age)
Write-Output "Age is $Age"
Drag options to blanks, or click blank then click option'
Aint
Bstring
Cbool
Darray
Attempts:
3 left
💡 Hint
Common Mistakes
Using [string] instead of [int] allows any text, not just numbers.
Using [bool] or [array] does not match the expected integer type.
2fill in blank
medium

Complete the code to validate that the parameter $Name is mandatory.

PowerShell
param(
    [Parameter([1]=$true)]
    [string]$Name
)
Write-Output "Name is $Name"
Drag options to blanks, or click blank then click option'
APosition
BOptional
CMandatory
DValueFromPipeline
Attempts:
3 left
💡 Hint
Common Mistakes
Using Optional instead of Mandatory allows skipping the parameter.
Using Position or ValueFromPipeline does not enforce requirement.
3fill in blank
hard

Fix the error in the code to validate that $Score is between 0 and 100.

PowerShell
param(
    [ValidateRange([1], 100)]
    [int]$Score
)
Write-Output "Score is $Score"
Drag options to blanks, or click blank then click option'
A1
B100
C-1
D0
Attempts:
3 left
💡 Hint
Common Mistakes
Using 1 as minimum excludes 0 which may be valid.
Using -1 allows negative scores which are invalid.
Using 100 as minimum is incorrect because max must be >= min.
4fill in blank
hard

Fill in the blank to validate that $Color is one of the allowed values.

PowerShell
param(
    [ValidateSet([1])]
    [string]$Color
)
Write-Output "Color is $Color"
Drag options to blanks, or click blank then click option'
A'Red', 'Green', 'Blue'
B'Yellow', 'Purple', 'Orange'
C'Circle', 'Square', 'Triangle'
D'Cat', 'Dog', 'Bird'
Attempts:
3 left
💡 Hint
Common Mistakes
Using unrelated sets like shapes or animals.
Not using quotes around the values.
5fill in blank
hard

Fill all three blanks to validate $Count is an integer between 1 and 10 and mandatory.

PowerShell
param(
    [Parameter([1]=$true)]
    [ValidateRange([2], [3])]
    [int]$Count
)
Write-Output "Count is $Count"
Drag options to blanks, or click blank then click option'
AMandatory
B1
C10
DOptional
Attempts:
3 left
💡 Hint
Common Mistakes
Setting Mandatory to false or Optional allows skipping the parameter.
Using wrong range values like 0 or 11.