0
0
PowerShellscripting~10 mins

Parameter attributes (Mandatory, ValidateSet) 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 make the parameter mandatory.

PowerShell
param(
    [[1](Mandatory=$true)]
    [string]$Name
)
Drag options to blanks, or click blank then click option'
AParameter
BOptional
CValidateSet
DMandatory
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'Mandatory' instead of 'Parameter' will cause a syntax error.
Using 'ValidateSet' here is incorrect because it restricts values, not requirement.
2fill in blank
medium

Complete the code to restrict the parameter to specific values.

PowerShell
param(
    [ValidateSet([1])]
    [string]$Color
)
Drag options to blanks, or click blank then click option'
A'Circle', 'Square', 'Triangle'
B'Red', 'Yellow', 'Pink'
C'Red', 'Green', 'Blue'
D'Apple', 'Banana', 'Cherry'
Attempts:
3 left
💡 Hint
Common Mistakes
Choosing unrelated sets like shapes or fruits will cause validation errors.
Leaving the set empty will not restrict values.
3fill in blank
hard

Fix the error in the parameter attribute to make the parameter mandatory with a ValidateSet.

PowerShell
param(
    [[1](Mandatory=$true)]
    [ValidateSet('Small', 'Medium', 'Large')]
    [string]$Size
)
Drag options to blanks, or click blank then click option'
AValidateSet
BMandatory
CValidateRange
DParameter
Attempts:
3 left
💡 Hint
Common Mistakes
Using Mandatory attribute alone does not accept parameters like Mandatory=$true.
Putting Mandatory=$true inside ValidateSet causes syntax errors.
4fill in blank
hard

Fill both blanks to create a mandatory parameter that only accepts 'Yes' or 'No'.

PowerShell
param(
    [[1](Mandatory=$true)]
    [[2]('Yes', 'No')]
    [string]$Confirm
)
Drag options to blanks, or click blank then click option'
AParameter
BMandatory
CValidateSet
DValidateRange
Attempts:
3 left
💡 Hint
Common Mistakes
Using Mandatory attribute alone without Parameter causes errors.
Using ValidateRange instead of ValidateSet will not restrict to specific strings.
5fill in blank
hard

Fill all three blanks to define a mandatory parameter named 'Mode' that accepts only 'Auto', 'Manual', or 'Off'.

PowerShell
param(
    [[1](Mandatory=[2])]
    [[3]('Auto', 'Manual', 'Off')]
    [string]$Mode
)
Drag options to blanks, or click blank then click option'
AParameter
B$true
CValidateSet
DMandatory
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'Mandatory' as an attribute alone is incorrect syntax.
Using '$false' instead of '$true' will make the parameter optional.
Using ValidateRange instead of ValidateSet will not restrict to specific strings.