0
0
PowerShellscripting~5 mins

Parameter attributes (Mandatory, ValidateSet) in PowerShell - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What does the Mandatory attribute do in a PowerShell parameter?
The Mandatory attribute makes a parameter required. PowerShell will ask the user to provide a value if it is not supplied when running the script or function.
Click to reveal answer
beginner
What is the purpose of the ValidateSet attribute in PowerShell parameters?
The ValidateSet attribute restricts the parameter's input to a predefined list of allowed values. This helps prevent errors by limiting choices to valid options.
Click to reveal answer
intermediate
How do you define a mandatory parameter named Color that only accepts 'Red', 'Green', or 'Blue'?
Use [Parameter(Mandatory=$true)][ValidateSet('Red','Green','Blue')]$Color in the parameter block to make it required and limit choices.
Click to reveal answer
beginner
What happens if a user provides a value not in the ValidateSet list?
PowerShell throws an error and stops execution, telling the user the value is invalid and showing the allowed options.
Click to reveal answer
beginner
Can you combine Mandatory and ValidateSet attributes on the same parameter?
Yes, combining them ensures the parameter is required and only accepts specific values from the set.
Click to reveal answer
What does the Mandatory attribute do in a PowerShell parameter?
AValidates the parameter type
BLimits the parameter to specific values
CMakes the parameter optional
DMakes the parameter required to be provided
Which attribute restricts a parameter's input to a fixed list of values?
AMandatory
BValidateRange
CValidateSet
DParameter
What will happen if a user omits a parameter marked as Mandatory?
APowerShell will assign a default value
BPowerShell will prompt the user to enter a value
CPowerShell will ignore the parameter
DPowerShell will throw an error immediately
If a parameter has ValidateSet('Red','Green','Blue'), what happens if the user inputs 'Yellow'?
APowerShell throws an error about invalid value
BPowerShell ignores the parameter
CPowerShell converts 'Yellow' to 'Red'
DThe script runs normally
How do you make a parameter both required and limited to specific values?
AUse both <code>[Parameter(Mandatory=$true)]</code> and <code>[ValidateSet()]</code>
BUse <code>[ValidateSet()]</code> only
CUse <code>[Parameter(Mandatory=$true)]</code> only
DUse <code>[MandatorySet()]</code>
Explain how the Mandatory and ValidateSet attributes work together in PowerShell parameters.
Think about forcing the user to choose from specific options.
You got /3 concepts.
    Describe what happens when a user runs a script without providing a mandatory parameter that has a ValidateSet.
    Consider the user experience when input is missing or wrong.
    You got /3 concepts.