Working with parameters
Parameters are values for script variables that can be passed when we run the script, rather than being hardcoded into the script. As we’ve just seen in the previous section, we run the script in much the same way as we run any other cmdlet, and, as with cmdlets, we can pass parameter values to our script. Let’s look at how we do that.
The first thing we need is the CmdletBinding
attribute. Attributes are a way of telling PowerShell how we want it to process the elements of our script. We’ve used them before in Chapter 4, PowerShell Variables and Data Structures, when we learned how to cast variables. The CmdletBinding
attribute tells PowerShell that we want it to treat the script as a cmdlet. Some of the big advantages this has are that our script will be able to access common parameters such as -Verbose
, as well as positional binding. Let’s add it. At the top of our script, create a new line and add this:
[CmdletBinding()...