Learn about Windows PowerShell
Summary: Microsoft Scripting Guy, Ed Wilson, talks about using named arguments in Windows PowerShell functions.
Microsoft Scripting Guy, Ed Wilson, is here. If I go to the trouble of writing a Windows PowerShell script, I generally do not use unnamed arguments (such as $args as I illustrated yesterday in Accepting Arguments for PowerShell Functions: Best Practices). Instead I create named arguments for my functions. It is just so much more powerful, and so much more flexible. Besides, I can still pass values appositionally in an unnamed fashion if I wish to do so.
In yesterday's blog, I said that there are only three requirements to create a function in Windows PowerShell:
To create a named argument in a Windows PowerShell function, I need only two additional things:
The following script illustrates this technique:
"This value of `$myargument is $myargument"
To use MyFunction, I first have to run the script. This loads the function into memory and makes it available via the function PSDrive. Because I have not saved the script containing the function, when I run the script, it appears in the Console pane below the Script pane. When the script runs, the Windows PowerShell prompt returns, and I can call the function by typing the name of the function. I then supply a value for the argument by typing it. This is shown in the image that follows.
Keep in mind that tab expansion works here. So I do not have to type the entire name of MyFunction, nor do I need to type the complete name of MyArgument. In fact, I only had to type my and press the Tab key to get the MyFunction command onto the command line. When I type the hyphen (–) for the named argument (parameter) a pop-up list appears, as shown in the following image.
The advantage of using named arguments (parameters) is that I do not need to name the parameter if I do not want to. I can use it as a positional parameter. In this manner, it behaves like an unnamed argument ($args). This is shown here.
PS C:\> myfunction "this is a string"
This value of $myargument is this is a string
Because creating named parameters in Windows PowerShell is so easy, and because using the Param keyword is the entry into the world of advanced functions, I never use $args in a Windows PowerShell script. Because it is an automatic variable that becomes available in certain circumstances, using $args is more difficult to understand because nothing has been created in the script. It is just there.
On the other hand, because the Param block is declared and available for inspection, it makes sense, and is easier to understand. If I begin with a script that uses $args and I later decide that I need to add functionality, I will have to add a Param block to get access to advanced features.
Join me tomorrow when I will welcome guest bloggers Yuri Diogenes and Tom Shinder back with the second installment in their security series. If you want to refresh your memory, check out their first installment in the series:
Security Series: Using PowerShell to Protect Your Private Cloud Infrastructure
I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at email@example.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.
Ed Wilson, Microsoft Scripting Guy
So then, how do you make it positional? Is it as simple as making the default value $args or whichever you want it to be?
Use the 'Position' attribute in the Param() block:
When I run the $env:COMPUTERNAME | myfunctions no error arises, but no value passes either. So what is the different between using named and unnamed parameters when you provide positional input to the function as neither gives error or pass value?