Bird
Raised Fist0
PowerShellscripting~10 mins

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

Choose your learning style10 modes available
Concept Flow - Parameter attributes (Mandatory, ValidateSet)
Define function with parameters
↓
Add Mandatory attribute?
Yes→Prompt user for input if missing
↓
User inputs value
↓
Add ValidateSet attribute?
Yes→Check 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.

Practice

(1/5)
1. What does the [Parameter(Mandatory=$true)] attribute do in a PowerShell script parameter?
easy
A. It limits the parameter to specific allowed values.
B. It sets a default value for the parameter.
C. It makes the parameter optional.
D. It makes the parameter required when running the script.

Solution

  1. Step 1: Understand the Mandatory attribute

    The Mandatory=$true setting means the parameter must be provided by the user when running the script.
  2. Step 2: Differentiate from other attributes

    Unlike ValidateSet, which restricts values, Mandatory only requires the parameter to be present.
  3. Final Answer:

    It makes the parameter required when running the script. -> Option D
  4. Quick Check:

    Mandatory = required parameter [OK]
Hint: Mandatory means you must provide the parameter value [OK]
Common Mistakes:
  • Confusing Mandatory with ValidateSet
  • Thinking Mandatory sets default values
  • Assuming Mandatory makes parameter optional
2. Which of the following is the correct syntax to restrict a mandatory parameter to only accept the values 'Red', 'Green', or 'Blue' in PowerShell?
easy
A. [Parameter(Mandatory=$true)][ValidateSet('Red','Green','Blue')]$Color
B. [ValidateSet('Red','Green','Blue')][Parameter()]$Color
C. [Parameter()][ValidateSet('Red','Green','Blue')]$Color
D. [ValidateSet('Red','Green','Blue')]$Color

Solution

  1. Step 1: Check attribute order and syntax

    Both Parameter and ValidateSet attributes can be used together. The order does not affect functionality, but the common style is to put Parameter first.
  2. Step 2: Confirm Mandatory and ValidateSet usage

    [Parameter(Mandatory=$true)][ValidateSet('Red','Green','Blue')]$Color correctly uses Mandatory=$true and ValidateSet with the allowed values.
  3. Final Answer:

    [Parameter(Mandatory=$true)][ValidateSet('Red','Green','Blue')]$Color -> Option A
  4. Quick Check:

    Correct syntax = [Parameter(Mandatory=$true)][ValidateSet('Red','Green','Blue')]$Color [OK]
Hint: Use [Parameter()] before [ValidateSet()] for clarity [OK]
Common Mistakes:
  • Swapping attribute order causing confusion
  • Missing parentheses in ValidateSet
  • Not setting Mandatory when required
3. Given the script snippet:
function Test-Color {
  param(
    [Parameter(Mandatory=$true)]
    [ValidateSet('Red','Green','Blue')]
    [string]$Color
  )
  Write-Output "Color chosen: $Color"
}

Test-Color -Color Yellow

What will happen when you run this script?
medium
A. It prompts the user to enter a valid color.
B. It outputs: Color chosen: Yellow
C. It throws a validation error because 'Yellow' is not in the ValidateSet.
D. It runs without error but ignores the color value.

Solution

  1. Step 1: Understand ValidateSet behavior

    The ValidateSet attribute restricts input to the listed values only. 'Yellow' is not in the set 'Red', 'Green', 'Blue'.
  2. Step 2: Predict script behavior on invalid input

    Passing 'Yellow' causes a validation error and stops the script before output.
  3. Final Answer:

    It throws a validation error because 'Yellow' is not in the ValidateSet. -> Option C
  4. Quick Check:

    ValidateSet blocks invalid values [OK]
Hint: ValidateSet blocks values not listed [OK]
Common Mistakes:
  • Assuming script prompts instead of erroring
  • Thinking it ignores invalid values
  • Confusing Mandatory with ValidateSet behavior
4. You wrote this parameter declaration:
param(
  [Parameter(Mandatory=$true)]
  [ValidateSet('Small','Medium','Large')]
  [string]$Size
)

But when running the script without the -Size argument, it does not prompt for input and continues silently. What is the likely cause?
medium
A. The parameter type should be [int] instead of [string].
B. The script is missing CmdletBinding() to enable parameter validation.
C. Mandatory parameters never prompt for input automatically.
D. The ValidateSet attribute is incorrect and disables Mandatory.

Solution

  1. Step 1: Understand Mandatory prompt behavior

    Mandatory parameters prompt for input only if the function or script uses CmdletBinding() or is an advanced function.
  2. Step 2: Identify missing CmdletBinding()

    Without CmdletBinding(), the script treats parameters as simple variables and does not enforce Mandatory prompts.
  3. Final Answer:

    The script is missing CmdletBinding() to enable parameter validation. -> Option B
  4. Quick Check:

    CmdletBinding() enables Mandatory prompts [OK]
Hint: Add CmdletBinding() to enable Mandatory prompts [OK]
Common Mistakes:
  • Thinking ValidateSet disables Mandatory
  • Assuming Mandatory never prompts
  • Changing parameter type unnecessarily
5. You want to create a script parameter that is required and only accepts the values 'Start', 'Stop', or 'Restart'. Which of the following parameter declarations correctly enforces both requirements and will prompt the user if the parameter is missing?
hard
A. [CmdletBinding()] param( [Parameter(Mandatory=$true)] [ValidateSet('Start','Stop','Restart')] [string]$Action )
B. param( [Parameter()] [ValidateSet('Start','Stop','Restart')] [string]$Action = 'Start' )
C. [CmdletBinding()] param( [Parameter()] [ValidateSet('Start','Stop','Restart')] [string]$Action )
D. param( [Parameter(Mandatory=$true)] [string]$Action )

Solution

  1. Step 1: Ensure Mandatory prompts user

    To prompt the user when a parameter is missing, the script must use CmdletBinding() and set Mandatory=$true.
  2. Step 2: Restrict values with ValidateSet

    Using ValidateSet limits input to the allowed values 'Start', 'Stop', or 'Restart'.
  3. Step 3: Check all options

    [CmdletBinding()] param( [Parameter(Mandatory=$true)] [ValidateSet('Start','Stop','Restart')] [string]$Action ) includes CmdletBinding(), Mandatory=$true, and ValidateSet, fulfilling all requirements.
  4. Final Answer:

    [CmdletBinding()] param( [Parameter(Mandatory=$true)] [ValidateSet('Start','Stop','Restart')] [string]$Action ) -> Option A
  5. Quick Check:

    CmdletBinding + Mandatory + ValidateSet = [CmdletBinding()] param( [Parameter(Mandatory=$true)] [ValidateSet('Start','Stop','Restart')] [string]$Action ) [OK]
Hint: Use CmdletBinding with Mandatory and ValidateSet together [OK]
Common Mistakes:
  • Omitting CmdletBinding() so no prompt occurs
  • Setting default value disables Mandatory prompt
  • Not using ValidateSet to restrict values
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard