Challenge - 5 Problems
Parameter Attributes Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate2:00remaining
Output of a script with Mandatory and ValidateSet attributes
What will be the output when running this PowerShell script without any parameters?
PowerShell
function Test-Param { param( [Parameter(Mandatory=$true)] [ValidateSet('Red','Green','Blue')] [string]$Color ) "Selected color is $Color" } Test-Param
Attempts:
2 left
💡 Hint
Think about what happens when a mandatory parameter is not provided.
✗ Incorrect
The parameter 'Color' is mandatory, so running the function without providing it causes PowerShell to prompt for it or throw an error if running non-interactively.
💻 Command Output
intermediate2:00remaining
Output when passing invalid value to ValidateSet parameter
What happens when you run this script with the parameter value 'Yellow'?
PowerShell
function Test-Param { param( [Parameter(Mandatory=$true)] [ValidateSet('Red','Green','Blue')] [string]$Color ) "Selected color is $Color" } Test-Param -Color Yellow
Attempts:
2 left
💡 Hint
Check what ValidateSet does when an invalid value is passed.
✗ Incorrect
ValidateSet restricts the parameter to specific values. Passing 'Yellow' causes a validation error.
📝 Syntax
advanced2:00remaining
Identify the correct syntax for Mandatory and ValidateSet attributes
Which option correctly declares a parameter named 'Mode' that is mandatory and accepts only 'Auto', 'Manual', or 'Off'?
Attempts:
2 left
💡 Hint
Mandatory attribute requires explicit assignment to $true or $false.
✗ Incorrect
The correct syntax uses Mandatory=$true explicitly. Option A and C omit the assignment, causing syntax errors. Option A sets Mandatory to false, which is incorrect.
🚀 Application
advanced2:00remaining
Determine the output of a script using Mandatory and ValidateSet with default value
What will be the output of this script when run without parameters?
PowerShell
function Test-Param { param( [Parameter(Mandatory=$true)] [ValidateSet('Small','Medium','Large')] [string]$Size = 'Medium' ) "Selected size is $Size" } Test-Param
Attempts:
2 left
💡 Hint
Consider how Mandatory and default values interact.
✗ Incorrect
A parameter cannot have both Mandatory=$true and a default value. PowerShell will still require the parameter to be provided.
🧠 Conceptual
expert2:00remaining
Understanding behavior of Mandatory and ValidateSet in pipeline input
Given this function, what will be the output when piping the string 'Green' to it?
PowerShell
function Test-Param { param( [Parameter(Mandatory=$true, ValueFromPipeline=$true)] [ValidateSet('Red','Green','Blue')] [string]$Color ) process { "Selected color is $Color" } } 'Green' | Test-Param
Attempts:
2 left
💡 Hint
Think about how pipeline input works with ValidateSet and Mandatory.
✗ Incorrect
The string 'Green' is passed through the pipeline and matches the ValidateSet, so the function outputs the selected color.