Input Parameters and Validation in PowerShell

When writing or re-organizing PowerShell scripts, one of the first improvements I usually make is replacing hardcoded values with input parameters. At the beginning of my PowerShell journey I often wrote scripts where usernames, server names, or paths were directly written into the script itself.

This works for quick tests, but very quickly those scripts become difficult to reuse. Every time something changes, the script has to be edited again. Using parameters solves this problem and turns a simple script into a flexible tool.

Another important aspect is parameter validation. Instead of checking values later in the script, PowerShell can validate them before the script even starts executing. This prevents many common errors and makes scripts much more reliable.

Basic Input Parameters

Input parameters are defined using a param block at the beginning of a script.


param(
    [string]$UserName,
    [string]$Department
)

Write-Output "Creating user $UserName in department $Department"

Now the script can be executed like this:


.\CreateUser.ps1 -UserName "Ahmed" -Department "IT"

Instead of editing the script every time, we simply pass different values as parameters.

Making Parameters Mandatory

Sometimes a parameter should always be provided. In those cases I mark it as mandatory.


param(
    [Parameter(Mandatory = $true)]
    [string]$UserName
)

If the parameter is not supplied, PowerShell will automatically prompt for it. This is a simple but very effective way to avoid incomplete input.

Using ValidateSet

One validation attribute I use very often is ValidateSet. It restricts input to a predefined list of allowed values.


param(
    [ValidateSet("Start","Stop","Restart")]
    [string]$Action
)

Valid execution:


.\ServiceScript.ps1 -Action Start

Invalid execution:


.\ServiceScript.ps1 -Action Delete

In this case PowerShell immediately throws an error because the value is not part of the allowed set. This prevents unexpected input from reaching the script logic.

Using ValidateNotNullOrEmpty

Another very useful validation is ValidateNotNullOrEmpty.


param(
    [ValidateNotNullOrEmpty()]
    [string]$UserName
)

This ensures that the parameter actually contains a value. Without this validation it is easy to accidentally pass an empty string.

Using ValidateRange

For numeric values PowerShell allows defining a valid range.


param(
    [ValidateRange(1,10)]
    [int]$RetryCount
)

This is very useful for things like retry attempts, limits, or timeout values.

Using ValidatePattern

If a parameter must follow a specific format, ValidatePattern can be used.


param(
    [ValidatePattern("^[a-zA-Z0-9]+$")]
    [string]$UserName
)

This example only allows alphanumeric characters. It can be useful for usernames, IDs, or naming conventions.

Conclusion

In my experience, using parameters together with validation is one of the easiest ways to improve the quality of PowerShell scripts. Instead of relying on manual checks inside the script, PowerShell ensures that only valid input is accepted.

This small change turns simple scripts into reusable tools and helps avoid many common scripting mistakes.

Leave a comment