Parameter validation helps check if the inputs to a script or function are correct before running it. This stops errors early and makes scripts safer.
Parameter validation in PowerShell
Start learning this pattern below
Jump into concepts and practice - no test required
function My-Function { param( [Parameter(Mandatory=$true)] [ValidateNotNullOrEmpty()] [string]$Name, [ValidateRange(1, 10)] [int]$Count ) # Function code here }
Use [Parameter(Mandatory=$true)] to require a parameter.
Validation attributes like [ValidateRange] or [ValidateNotNullOrEmpty] check input before the script runs.
function Get-UserAge { param( [ValidateRange(0, 120)] [int]$Age ) "User age is $Age" }
function Set-FilePath { param( [ValidateScript({Test-Path $_})] [string]$Path ) "File path is valid: $Path" }
function Choose-Color { param( [ValidateSet('Red', 'Green', 'Blue')] [string]$Color ) "You chose $Color" }
This script greets a person by name a certain number of times. It checks that the name is not empty and the number of times is between 1 and 5.
function Greet-Person { param( [Parameter(Mandatory=$true)] [ValidateNotNullOrEmpty()] [string]$Name, [ValidateRange(1, 5)] [int]$Times = 1 ) for ($i = 1; $i -le $Times; $i++) { Write-Output "Hello, $Name!" } } # Call the function Greet-Person -Name "Alice" -Times 3
If validation fails, PowerShell shows an error and stops the script.
You can combine multiple validation attributes for stronger checks.
Default values can be set for parameters to make them optional.
Parameter validation checks inputs before running code.
Use attributes like [ValidateRange], [ValidateSet], and [ValidateNotNullOrEmpty].
Validation helps avoid errors and makes scripts safer and easier to use.
Practice
Solution
Step 1: Understand parameter validation role
Parameter validation ensures inputs meet certain rules before the script runs.Step 2: Identify the main benefit
This prevents errors caused by bad inputs and makes scripts safer.Final Answer:
To check input values before the script runs to avoid errors -> Option CQuick Check:
Parameter validation = input checking [OK]
- Thinking validation speeds up script
- Confusing validation with output formatting
- Assuming validation creates variables
Solution
Step 1: Identify correct validation attribute for fixed sets
[ValidateSet] restricts input to specific allowed values.Step 2: Match syntax with attribute usage
The syntax [ValidateSet('Red','Green','Blue')] is correct for this purpose.Final Answer:
[ValidateSet('Red','Green','Blue')] param([string]$Color) -> Option AQuick Check:
Fixed set validation = ValidateSet [OK]
- Using ValidateRange for strings
- Confusing ValidateNotNullOrEmpty with value restriction
- Using ValidatePattern incorrectly for sets
-Age 25?
function Test-Age {
param(
[ValidateRange(18,30)]
[int]$Age
)
"Age is $Age"
}
Test-Age -Age 25Solution
Step 1: Check parameter validation range
The parameter $Age must be between 18 and 30 inclusive.Step 2: Verify input against range
Input 25 is within the range, so validation passes and script runs.Final Answer:
"Age is 25" -> Option DQuick Check:
25 in 18-30 range = valid input [OK]
- Assuming validation fails for valid input
- Confusing output with error message
- Ignoring the inclusive range
function Set-Name {
param(
[ValidateNotNullOrEmpty()]
[string]$Name
)
"Name set to $Name"
}
Set-Name -Name $nullSolution
Step 1: Understand ValidateNotNullOrEmpty behavior
This attribute rejects null or empty string inputs.Step 2: Analyze input value
The input is $null, which violates the validation rule, causing an error.Final Answer:
The function will throw a validation error because $Name is null -> Option BQuick Check:
Null input with ValidateNotNullOrEmpty = error [OK]
- Thinking null is accepted
- Assuming ValidateNotNullOrEmpty needs parameters
- Confusing parameter type requirements
$Port that only accepts integers between 1024 and 65535 and cannot be empty. Which parameter validation attributes should you use together?Solution
Step 1: Choose attribute for numeric range
[ValidateRange(1024,65535)] ensures the integer is within the port number range.Step 2: Ensure parameter is not empty
[ValidateNotNullOrEmpty()] prevents null or empty input.Step 3: Combine both for full validation
Using both attributes together enforces the correct range and non-empty input.Final Answer:
[ValidateRange(1024,65535)][ValidateNotNullOrEmpty()] -> Option AQuick Check:
Range + NotNullOrEmpty = correct port validation [OK]
- Using ValidateSet for numeric ranges
- Relying on ValidatePattern for numeric range checks
- Missing non-empty validation
