Bird
Raised Fist0
PowerShellscripting~20 mins

Parameter validation in PowerShell - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Challenge - 5 Problems
🎖️
PowerShell Parameter Validation Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
2: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
ANo output, function silently ignores invalid input
BNumber is 15
CValidationException: Cannot validate argument on parameter 'Number'. The argument 15 is less than the minimum allowed range of 1.
DValidationException: Cannot validate argument on parameter 'Number'. The argument 15 is greater than the maximum allowed range of 10.
Attempts:
2 left
💡 Hint
Think about what happens when a parameter value is outside the allowed range in PowerShell parameter validation.
💻 Command Output
intermediate
2: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'
AValidationException: Cannot validate argument on parameter 'Name'. The argument 'John123' does not match the pattern '^[a-zA-Z]+$'.
BNo output, function returns null
CRuntimeException: Invalid characters in parameter 'Name'.
DName is John123
Attempts:
2 left
💡 Hint
Look at the regex pattern used in ValidatePattern and what input is given.
📝 Syntax
advanced
2: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"
}
A
[ValidateSet('Red','Green','Blue')]
[int]$Color
B
[ValidateSet('Red','Green','Blue')]
[string]$Color
C
[ValidateSet('Red','Green','Blue')]
[bool]$Color
D
[ValidateSet('Red','Green','Blue')]
[array]$Color
Attempts:
2 left
💡 Hint
Think about the data type that matches color names.
🚀 Application
advanced
2: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
AAdult
BYoung Adult
CValidationException: Age is out of range
DNo output
Attempts:
2 left
💡 Hint
Check the range and the if condition inside the function.
🧠 Conceptual
expert
2: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?
A[ValidateNotNull()]
B[ValidateNotEmpty()]
C[ValidateNotNullOrEmpty()]
D[ValidateString()]
Attempts:
2 left
💡 Hint
One attribute specifically checks for both null and empty strings.

Practice

(1/5)
1. What is the main purpose of parameter validation in a PowerShell script?
easy
A. To speed up the script execution
B. To format the output of the script
C. To check input values before the script runs to avoid errors
D. To create new variables automatically

Solution

  1. Step 1: Understand parameter validation role

    Parameter validation ensures inputs meet certain rules before the script runs.
  2. Step 2: Identify the main benefit

    This prevents errors caused by bad inputs and makes scripts safer.
  3. Final Answer:

    To check input values before the script runs to avoid errors -> Option C
  4. Quick Check:

    Parameter validation = input checking [OK]
Hint: Validation checks inputs early to prevent errors [OK]
Common Mistakes:
  • Thinking validation speeds up script
  • Confusing validation with output formatting
  • Assuming validation creates variables
2. Which of the following is the correct syntax to validate that a parameter only accepts values from a fixed set: 'Red', 'Green', or 'Blue'?
easy
A. [ValidateSet('Red','Green','Blue')] param([string]$Color)
B. [ValidateRange('Red','Green','Blue')] param([string]$Color)
C. [ValidateNotNullOrEmpty('Red','Green','Blue')] param([string]$Color)
D. [ValidatePattern('Red|Green|Blue')] param([string]$Color)

Solution

  1. Step 1: Identify correct validation attribute for fixed sets

    [ValidateSet] restricts input to specific allowed values.
  2. Step 2: Match syntax with attribute usage

    The syntax [ValidateSet('Red','Green','Blue')] is correct for this purpose.
  3. Final Answer:

    [ValidateSet('Red','Green','Blue')] param([string]$Color) -> Option A
  4. Quick Check:

    Fixed set validation = ValidateSet [OK]
Hint: Use ValidateSet for fixed allowed values [OK]
Common Mistakes:
  • Using ValidateRange for strings
  • Confusing ValidateNotNullOrEmpty with value restriction
  • Using ValidatePattern incorrectly for sets
3. What will be the output of this script if called with -Age 25?
function Test-Age {
  param(
    [ValidateRange(18,30)]
    [int]$Age
  )
  "Age is $Age"
}
Test-Age -Age 25
medium
A. "Age is 18"
B. Error: Parameter validation failed
C. No output
D. "Age is 25"

Solution

  1. Step 1: Check parameter validation range

    The parameter $Age must be between 18 and 30 inclusive.
  2. Step 2: Verify input against range

    Input 25 is within the range, so validation passes and script runs.
  3. Final Answer:

    "Age is 25" -> Option D
  4. Quick Check:

    25 in 18-30 range = valid input [OK]
Hint: Check if input fits ValidateRange limits [OK]
Common Mistakes:
  • Assuming validation fails for valid input
  • Confusing output with error message
  • Ignoring the inclusive range
4. Identify the error in this parameter validation code:
function Set-Name {
  param(
    [ValidateNotNullOrEmpty()]
    [string]$Name
  )
  "Name set to $Name"
}
Set-Name -Name $null
medium
A. The parameter type should be [int] instead of [string]
B. The function will throw a validation error because $Name is null
C. The ValidateNotNullOrEmpty attribute is used incorrectly without parameters
D. The function will accept null and print 'Name set to '

Solution

  1. Step 1: Understand ValidateNotNullOrEmpty behavior

    This attribute rejects null or empty string inputs.
  2. Step 2: Analyze input value

    The input is $null, which violates the validation rule, causing an error.
  3. Final Answer:

    The function will throw a validation error because $Name is null -> Option B
  4. Quick Check:

    Null input with ValidateNotNullOrEmpty = error [OK]
Hint: Null or empty inputs fail ValidateNotNullOrEmpty [OK]
Common Mistakes:
  • Thinking null is accepted
  • Assuming ValidateNotNullOrEmpty needs parameters
  • Confusing parameter type requirements
5. You want to create a script parameter $Port that only accepts integers between 1024 and 65535 and cannot be empty. Which parameter validation attributes should you use together?
hard
A. [ValidateRange(1024,65535)][ValidateNotNullOrEmpty()]
B. [ValidateSet(1024,65535)][ValidateNotNullOrEmpty()]
C. [ValidatePattern('^[0-9]{4,5}$')]
D. [ValidateNotNullOrEmpty()][ValidateLength(4,5)]

Solution

  1. Step 1: Choose attribute for numeric range

    [ValidateRange(1024,65535)] ensures the integer is within the port number range.
  2. Step 2: Ensure parameter is not empty

    [ValidateNotNullOrEmpty()] prevents null or empty input.
  3. Step 3: Combine both for full validation

    Using both attributes together enforces the correct range and non-empty input.
  4. Final Answer:

    [ValidateRange(1024,65535)][ValidateNotNullOrEmpty()] -> Option A
  5. Quick Check:

    Range + NotNullOrEmpty = correct port validation [OK]
Hint: Combine ValidateRange and ValidateNotNullOrEmpty for numeric required input [OK]
Common Mistakes:
  • Using ValidateSet for numeric ranges
  • Relying on ValidatePattern for numeric range checks
  • Missing non-empty validation