0
0
PowerShellscripting~15 mins

Parameter validation in PowerShell - Mini Project: Build & Apply

Choose your learning style9 modes available
Parameter validation
📖 Scenario: You are creating a PowerShell script that accepts user input as parameters. To make sure the input is correct and safe, you will add parameter validation rules.This is like checking the ingredients before cooking to ensure the recipe works well.
🎯 Goal: Build a PowerShell script with parameters that have validation rules to accept only specific types and values.
📋 What You'll Learn
Create a script with a parameter named Name that accepts only strings.
Add a parameter named Age that accepts only integers between 18 and 99.
Add a parameter named Country that accepts only specific values: 'USA', 'Canada', or 'UK'.
Print the received parameter values.
💡 Why This Matters
🌍 Real World
Parameter validation helps prevent errors and ensures scripts run with correct input, just like checking ingredients before cooking.
💼 Career
Many IT and automation jobs require writing scripts that safely accept user input. Parameter validation is a key skill for reliable scripts.
Progress0 / 4 steps
1
Create parameters Name and Age
Write a PowerShell script that defines two parameters: Name which accepts only strings, and Age which accepts only integers.
PowerShell
Need a hint?

Use [string] and [int] before the parameter names inside param().

2
Add validation for Age to accept only 18 to 99
Modify the Age parameter to accept only integers between 18 and 99 using [ValidateRange(18, 99)].
PowerShell
Need a hint?

Use [ValidateRange(18, 99)] just before [int]$Age.

3
Add parameter Country with specific allowed values
Add a new parameter called Country that accepts only the values 'USA', 'Canada', or 'UK' using [ValidateSet('USA', 'Canada', 'UK')] and type string.
PowerShell
Need a hint?

Use [ValidateSet('USA', 'Canada', 'UK')] before [string]$Country.

4
Print the parameter values
Write code to print the values of Name, Age, and Country using Write-Host.
PowerShell
Need a hint?

Use Write-Host to display each parameter value on its own line.