0
0
PowerShellscripting~10 mins

Parameter attributes (Mandatory, ValidateSet) in PowerShell - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Parameter attributes (Mandatory, ValidateSet)
Define function with parameters
Add Mandatory attribute?
YesPrompt user for input if missing
User inputs value
Add ValidateSet attribute?
YesCheck input against allowed values
If invalid, show error
Run function with validated parameters
Function executes with correct inputs
This flow shows how PowerShell checks if parameters are mandatory and validates their values against a set of allowed options before running the function.
Execution Sample
PowerShell
function Test-Param {
  param(
    [Parameter(Mandatory=$true)]
    [ValidateSet('Red','Green','Blue')]
    [string]$Color
  )
  Write-Output "Color chosen: $Color"
}
Defines a function with a mandatory parameter 'Color' that must be one of 'Red', 'Green', or 'Blue'.
Execution Table
StepActionInput ProvidedValidationResultOutput
1Call function without parameterNoneMandatory check failsPrompt user for input
2User inputs 'Yellow'YellowValidateSet failsError: Invalid value
3User inputs 'Green'GreenValidateSet passesFunction runsColor chosen: Green
4Call function with parameter 'Blue'BlueMandatory passes, ValidateSet passesFunction runsColor chosen: Blue
5Call function with parameter 'Red'RedMandatory passes, ValidateSet passesFunction runsColor chosen: Red
6Call function with parameter 'yellow'yellowValidateSet fails (case sensitive)Error: Invalid value
💡 Execution proceeds when a valid mandatory parameter matching the ValidateSet is provided.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4After Step 5After Step 6
$Colornullnull (missing)Yellow (invalid)Green (valid)Blue (valid)Red (valid)yellow (invalid)
Key Moments - 3 Insights
Why does the function prompt for input when called without parameters?
Because the parameter has the Mandatory attribute set to true, PowerShell requires a value and prompts the user if none is provided (see execution_table step 1).
Why does input 'Yellow' cause an error?
The ValidateSet attribute restricts allowed values to 'Red', 'Green', or 'Blue'. 'Yellow' is not in this set, so validation fails (see execution_table step 2).
Is the ValidateSet check case sensitive?
Yes, 'yellow' (lowercase) is invalid because it does not exactly match any allowed value (see execution_table step 6).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what happens at step 1 when the function is called without parameters?
AThe function runs with a default value
BAn error is shown immediately
CPowerShell prompts the user for input
DThe function runs with a null value
💡 Hint
Check the 'Result' column in execution_table row 1.
At which step does the function successfully run with a valid parameter without prompting?
AStep 4
BStep 2
CStep 1
DStep 6
💡 Hint
Look for 'Function runs' in the 'Result' column without prompting in execution_table.
If the ValidateSet included 'Yellow', how would step 2 change?
AIt would still show an error
BThe function would run successfully with 'Yellow'
CIt would prompt the user again
DThe function would ignore the parameter
💡 Hint
Consider how ValidateSet controls allowed values in execution_table step 2.
Concept Snapshot
PowerShell parameter attributes:
- Mandatory=$true forces user input if missing
- ValidateSet restricts input to specific allowed values
- If Mandatory and ValidateSet are used, PowerShell prompts for input and validates it
- Invalid input causes an error and stops execution
- ValidateSet is case sensitive by default
Full Transcript
This example shows a PowerShell function with a parameter that is mandatory and must be one of a set of allowed values. When the function is called without the parameter, PowerShell prompts the user to enter a value because of the Mandatory attribute. If the user enters a value not in the allowed set, like 'Yellow', PowerShell shows an error and does not run the function. If the user enters a valid value like 'Green', the function runs and outputs the chosen color. The ValidateSet attribute ensures only specific values are accepted, and it is case sensitive. This helps prevent mistakes and ensures the function gets valid input before running.