Allowing Parameters to be Passed to a Powershell Script

A while back I was writing a powershell script when I realised I didn’t know how to allow parameters to be passed to a powershell script. Turns out it’s not that difficult. To allow named parameters to be passed in, the first line in your script must be the param function with comma separated variables that you would like to be passed in:

You can even provide defaults for your parameters just by setting a value in the param function:

You can also add required parameters, and throw an exception if they aren’t set:

Finally we can prompt for a value if we don’t receive one:


Leave a Reply