Changes

Jump to: navigation, search

Windows PowerShell 1.0 Functions

32 bytes added, 14:45, 15 May 2009
no edit summary
== Creating and Calling a Simple Windows PowerShell Function ==
The most basic of Windows PowerShell functions are created using the ''function'' keyword followed by the name of the function. Finally, the list of statements to be executed are is enclosed in curly braces. For example, the following function simply outputs a string before returning:
<pre>
== Passing Parameters to a Function ==
<google>ADSDAQBOX_FLOW</google>
In the preceding example, the function did not expect to be passed any parameters (also known as arguments). These are values which that are entered on the command-line along with the function at the point that it is called. Windows PowerShell stores any parameters that were entered in a special variable called ''$args''. The ''$args'' variable is actually an array object with each parameter assigned to an array element (for more detailed information on working with arrays refer to the chapter entitled [[Working with Arrays in Windows PowerShell 1.0]]).
In order to demonstrate the use of the ''$args'' variable in the context of Windows PowerShell functions, the following function displays the number of parameters passed into the function, and then multiplies the first two parameters:
== Specifying Named Parameters ==
In the above example, we relied on the ''$args'' array to access any parameters which that were passed through to the function when it was called. Whilst this apprach approach works fine for simple functions, it can quickly become confusing as functions become more complex. An alternative mechanism is to declare the expected parameters and assign them variable names by which they may be accessed within the body of the function. This is achieved by placing the expected parameters in parentheses after the function name during the declaration. For example, the following function declares that two parameters are expected when the function is called, and names them "$firstname" and "$lastname" respectively.
<pre>
</pre>
Alternatively, the parameters may be specified using the names, which allows allow them to be passed in any order. In the following example, the last name parameter is specified before the first name. Because the parameter names are specified, however, the values are still assigned to the correct parameters:
<pre>
== Windows PowerShell Function Administration ==
Functions declared within PowerShell are stored in the ''function'' namespace which is mapped to a virtual ''function:'' drive. As such, functions can be manipulated and administered much in the same way files in a filesytem are handled. It is possible, for example, to list the ''multiply_nnumbersmultiply_numbers'' function created earlier in this chapter using the ''dir'' command:
<pre>
As illustrated above, the function is now retained in the Windows PowerShell session. This technique applies equally to variables and any other settings that may be included in a script file which is subsequently ''dotted''.
 
<google>BUY_WPS_BOTTOM</google>

Navigation menu