Challenge - 5 Problems
PowerShell Parameter Validation Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate2:00remaining
What is the output of this PowerShell function call with parameter validation?
Consider the following PowerShell function that validates its parameter. What will be the output when calling
Test-Param -Number 15?PowerShell
function Test-Param { param( [ValidateRange(1,10)] [int]$Number ) "Number is $Number" } Test-Param -Number 15
Attempts:
2 left
💡 Hint
Think about what happens when a parameter value is outside the allowed range in PowerShell parameter validation.
✗ Incorrect
The function uses [ValidateRange(1,10)] which restricts the parameter to values between 1 and 10. Passing 15 violates this, causing a ValidationException with a message about exceeding the maximum allowed range.
💻 Command Output
intermediate2:00remaining
What error does this PowerShell function produce when called with an invalid string?
Given this function with parameter validation, what error message appears when calling
Check-Name -Name 'John123'?PowerShell
function Check-Name { param( [ValidatePattern('^[a-zA-Z]+$')] [string]$Name ) "Name is $Name" } Check-Name -Name 'John123'
Attempts:
2 left
💡 Hint
Look at the regex pattern used in ValidatePattern and what input is given.
✗ Incorrect
The pattern '^[a-zA-Z]+$' allows only letters. 'John123' contains digits, so it fails validation and throws a ValidationException with a message about the pattern mismatch.
📝 Syntax
advanced2:00remaining
Which option correctly uses ValidateSet to restrict parameter values?
You want to create a function parameter that only accepts the values 'Red', 'Green', or 'Blue'. Which of the following parameter declarations is correct?
PowerShell
function Set-Color { param( # Parameter declaration here ) "Color set to $Color" }
Attempts:
2 left
💡 Hint
Think about the data type that matches color names.
✗ Incorrect
ValidateSet restricts the parameter to specific string values. The parameter type must be string to accept color names. Using int, bool, or array types will cause errors or unexpected behavior.
🚀 Application
advanced2:00remaining
What is the output of this function when called with
-Age 25?This function uses multiple parameter validations. What will it output when called as
Check-Age -Age 25?PowerShell
function Check-Age { param( [ValidateRange(18,30)] [ValidateNotNull()] [int]$Age ) if ($Age -ge 21) { "Adult" } else { "Young Adult" } } Check-Age -Age 25
Attempts:
2 left
💡 Hint
Check the range and the if condition inside the function.
✗ Incorrect
The parameter Age is validated to be between 18 and 30 and not null. 25 passes validation. Since 25 is greater or equal to 21, the function outputs 'Adult'.
🧠 Conceptual
expert2:00remaining
Which parameter validation attribute prevents null or empty strings?
You want to ensure a string parameter is not null or empty in your PowerShell function. Which validation attribute should you use?
Attempts:
2 left
💡 Hint
One attribute specifically checks for both null and empty strings.
✗ Incorrect
[ValidateNotNull()] only prevents null values but allows empty strings. [ValidateNotNullOrEmpty()] prevents both null and empty strings. The other options are not valid PowerShell validation attributes.