Q: What is the best way of passing parameters to my Windows PowerShell script?

A: Windows PowerShell has an easy way to pass parameters. The following first line accepts a parameter with  a default value.


If you don't want a default value but rather want to make the parameter mandatory, it can be changed to this:


PowerShell will automatically pass the user an error if they try to run the script without the parameter, with no need for any extra coding. By default, users can also use a parameter name, which will match the name of the variable, for example:

script.ps1 -computername <value>


You can set alternate aliases if you wish by also adding the [Alias('AliasName')] to the parameter.

We have a lot of FAQs on PowerShell. You might also be interested in PowerShell for managing Hyper-V and PowerShell for managing Active Directory

Hide comments


  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.