PowerShell Essentials (Part 7)

If you would like to read the other parts in this article series please go to:

In my previous article, I showed you how to create a function in Windows PowerShell. My original intent had been to end the series there. After writing that article however, I realized that there were a couple of other important things that I needed to talk about before ending this series.

One of those things that I still need to cover is the use of parameters. I showed you how to use a function, but the function that I used in my example was relatively static. In the real world, parameters are often passed to and from functions. Consequently, I want to spend the remainder of this article talking some more about PowerShell functions.

The other thing that I realized that I never talked about was the link between PowerShell and the .NET Framework. You can use .NET to do all kinds of cool things in PowerShell, so that’s what I plan to talk about in Part 8. So now that I have given you a preview of where I plan to go with this series, let’s get down to business.

As you may recall from the previous article, I created a simple function that checks the value of a variable to see if the number that is associated with the variable is greater than zero. The actual code that I used is shown below:

Function VarCheck
{
If ($X -GT 0){“Bigger than Zero”}

$X=4
VarCheck
$X=0
VarCheck
$X=1
VarCheck

The script sets a variable called $X to 4 and then calls the function VarCheck and checks to see if the variable’s contents (in this case 4) is bigger than 0. The script then sets the variable to 0, calls the VarCheck function and checks to see if the variable’s contents (in this case 0) is greater than 0. Finally, the script sets the variable’s contents to 1, calls the VarCheck function, and checks to see if the variable’s contents (in this case 1) is greater than 0. You can see the script’s output in Figure A below.

Image
Figure A: This is the output from my previously created PowerShell script.

So as you can see, the script works exactly as it is supposed to. When I wrote this script however, I wrote it in a way that I hoped would illustrate how to call a function, without being confusing in the process. In fact, the script represents the simplest way to use a function. Within the script I have created a named function called VarCheck and any time that I need to call the function I simply treat VarCheck as if it were a native command.

The method that I used gets the job done, but this isn’t the way that PowerShell functions usually work. As you saw in the script, I would declare a variable and then call the function. More often however, PowerShell scripts simply pass a value to a function rather than trying to use a variable that has been declared elsewhere (although the two methods can be combined). Let me show you how it works.

Let’s pretend that I need for my script to do the same thing that it does now, but that for whatever reason I want to pass values directly to my function rather than making use of a variable. If that is the goal then the function would need to be modified to look like the block of code shown below:

Function VarCheck($arg1)
{
If ($arg1 -GT 0){echo $arg1 ” is bigger than zero”}

VarCheck 4
VarCheck 0
VarCheck 1

This script pretty much does the same thing as the script that we created before, but there are two differences. The first difference is that now we are passing values to the function rather than declaring variables within the main body of code. The other difference is that I have changed the output slightly as a way of making the output more clear.

With that said, let’s step through the code and see how this script works. My first line of code defines a named function. Just like before, I am calling this function VarCheck. The thing that is different from my previous script is that the function name (in this case VarCheck) is followed by: ($Arg1)

This is how we tell PowerShell that we are going to be passing a parameter (sometimes called an argument, hence the Arg1 name) to the function. Now if you look closely, you will notice that $Arg1 is a variable because its first character is a dollar sign. This variable automatically inherits whatever value is passed to the function. Therefore, if we passed a value of 4 to the function then $arg1 would contain a value of 4.

The second and fourth lines of code contain a set of braces ({}). Remember, the code block that is associated with a PowerShell function is always enclosed in braces. These braces define the beginning and the end of the function’s code.

This particular function only uses a single line of code. It’s the line that appears between the two braces. This line if very similar to what we were using in the original function, but there are two changes. To more clearly illustrate those changes, let me show you both the old and the new version of this line of code:

This is how the line originally looked:

If ($X -GT 0){“Bigger than Zero”}

This is how the line looks now:

If ($arg1 -GT 0){echo $arg1 ” is bigger than zero”}

In both cases the line of code is checking a variable to see if it is greater than (-GT) zero. In the original version, the code checked the value of $X. As you will recall however, our new script doesn’t use the $X variable. We are passing values directly to the function and the function assigns those values to the variable $arg1. Therefore, the new function compares $arg1 to 0 rather than comparing $X to zero.

In both lines of code, we are essentially performing an if then. Both lines of code begin with an if and then establish a condition. The condition is enclosed in parenthesis and in each case compares the value of a variable against 0. The next part of the line is the “then”. This is the portion of code that is enclosed in braces. It’s what happens if the condition is true.

In our original code, the “then” portion of the line simply displayed the text “Bigger than Zero”. In the new version of the code, I have made a slight modification that displays the value of the $arg1 variable before displaying the “Bigger than Zero” text. I did this by using the word Echo, followed by the variable name. If you have ever done DOS batch programming, you may recall that Echo is a command used to display a line of text.

Now look below the function. You can see that all of my function calls are followed by a value. For example, VarCheck 4 passes the value of 4 to the function, where it is compared against zero. Although I am passing raw data to the function, you can use the same technique to pass a variable’s contents to a function. You can see the output of my script in Figure B.

Image
Figure B: This is the output from my script.

Conclusion

In this article, I have explained how you can pass values to a PowerShell function. In my next article, I want to show you how you can use PowerShell to tap into the power of .NET.

If you would like to read the other parts in this article series please go to:

About The Author

Leave a Comment

Your email address will not be published. Required fields are marked *

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

Scroll to Top