Make Function Parameters Mandatory

by Jul 7, 2010

Mandatory parameters are special because if you do not submit it, PowerShell will automatically ask for it. You can use mandatory parameters in your own functions as well. You should simply mark a function as mandatory by adding the appropriate [Parameter()] attribute like so:

function Test-Me {
param(
[Parameter(Mandatory=$true)]
$name
)
"You entered $name."
}

Test-Me

The parameter attributes used in this tip were introduced with PowerShell v2.