Using parameters for your Scripts and Functions is very powerful. You don’t have to hardcode things in them, making running them from a command line easier. This blog post will show you the parameters I use in most of my scripts and how they work.
HelpMessage

You can see that the scripts prompted for a value for the -Filename parameter, and it also tells you to use !? For Help. Typing !? Shows you the HelpMessage that you configured in the parameter, in this case it tells you to use a complete path to the filename with an example of c:\temp\powershellisfun.txt.
Mandatory
If you need to use a parameter, specify it using Mandatory = $true. If not, then you can use Mandatory = $false. For example:
param( [parameter(Mandatory = $true)][string[]]$ComputerName = $env:COMPUTERNAME )
In the example above, the -ComputerName parameter must be used. However, if no value is specified, it will automatically use $env:COMPUTERNAME as the value. If you don’t use the -ComputerName parameter and the Mandatory option is set to $true, it will automatically use $env:COMPUTERNAME.
Running this example will look like this:

ParameterSetname
If you have multiple parameters and a few that can’t be used together, you can use the ParameterSetName option. In the example script below, I have the -PowerShellEventlogOnly and -PSReadLineHistoryOnly parameters that can’t be used together. If you run the script and use the – key with Tab to show the parameters, they will show both. If you select one, try to choose the other. Then, you will see that it’s no longer available for usage.

Switch

You can see that running the switch.ps1 script with the -filename parameter will display the contents of the powershellisfun.txt on your screen. The -GridView parameter with a $false value will also be outputted on your screen. The -GridView parameter, with a default value of $true, will output the contents in an Out-Gridview pane.
ValidateSet
Using the ValidateSet option, you can specify a parameter with one of the configured values. This is easier than prompting or supplying values that might have typos. In the example below, I used a set of values for the state of a Windows Service that can be passed as a parameter.

You can see that when using the -StartupType parameter, the available values shown are the ones configured in the ValidateSet values. When I tried to type a value not in the list, valuenotinlist, in this case for testing, it threw an error that it’s not in the ValidateSet values. “The argument “valuenotinlist” does not belong to the set “Automatic,Boot,Disabled,Manual,System” specified by the ValidateSet attribute. Supply an argument that is in the set and then try the command again.”
Wrapping up
In this blog post, I showed you some examples of parameters that you can use in your scripts to make things more dynamic and easier. Hardcoding variables in scripts is not something you should do for scripts that could be used by more people other than you 😉