PowerShell Essentials (Part 6)

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

Throughout this article series, my goal has been to answer some of the most commonly asked questions about Windows PowerShell in an effort to give you a solid base of PowerShell knowledge. In this article, I want to wrap up the series by talking about scripts and functions.

Let’s start by talking about PowerShell scripts. A PowerShell script is really nothing more than a text file. The main idea behind PowerShell scripts is that you may need to automate tasks, and scripts allow you to do that without having to manually enter a series of command. Scripts are also useful when you want to create large blocks of code. In these types of situations, scripts not only make your code reusable, they also give you the ability to edit your code.

Before I show you how to create a PowerShell script, I need to take a moment and talk about execution policies. When PowerShell was first announced to the public (before it was ever made available), I remember writing that PowerShell was nothing short of a dream come true for malware authors. I went on to theorize that PowerShell would make it a lot easier to develop malicious code and that malware authors would soon be able to do things that they had previously only imagined.

The thing that kept my apocalyptic prophecy from coming true was the execution policy. An execution policy determines what types of PowerShell scripts can be run on a system. Microsoft provides four different types of execution policies including:

Restricted – This is the default execution policy. It does not allow scripts to be run. PowerShell cmdlets can only be entered manually.

AllSigned – Scripts can be run if they are signed by a trusted publisher.

RemoteSigned – Scripts can be downloaded and run if they are signed by a trusted publisher.

Unrestricted – There are no restrictions on running PowerShell scripts.

It is worth noting that it is possible to create execution policy scopes and to use the Bypass parameter to block an execution policy or to use the Undefined parameter to remove an execution policy from the current scope. Even so, the four execution policy settings listed above are the most commonly used. If you want more granular control over execution policies then I recommend checking out this TechNet article.

Setting an execution policy is simple. For instance, if you want to set the execution policy to unrestricted, you can use the following command:

Set-ExecutionPolicy Unrestricted

So with that said, let’s talk about scripts. As previously mentioned, a script is nothing more than a text file. The most commonly used file extension for PowerShell scripts is .PS1.

If you think back to the previous article in this series, you will recall that I showed you a couple of lines of code that set a variable called $X to a value of 4 and then later checked to see if X was greater than 0. If you look at Figure A, you can see that I have turned those commands into a script by copying them to Notepad and then saving the file with a .PS1 extension. For the sake of demonstration, I called the script MyScript.ps1.

Image
Figure A: This is an example of a PowerShell script.

So how do we go about executing a script. Well, the first step is to make sure that the execution policy allows the script to be executed. You can confirm the execution policy by using the Get-ExecutionPolicy cmdlet, as shown in Figure B.

Image
Figure B: You can use the Get-ExecutionPolicy cmdlet to verify the execution policy that is in effect.

Once you have verified the execution policy, you can run the script by navigating to the folder containing the script and then entering ./ followed by the script name. To show you what I mean, let’s run the MyScript.ps1 file. I saved my script to C:\Users\Brien\Documents. Therefore, the commands that I would use to change to the correct folder and then execute the script are:

CD \
CD Users\Brien\Documents
./MyScript.ps1

You can see that this looks like in Figure C.

Image
Figure C: You can use ./ followed by the script name to execute a script.

So now that I have shown you how to create and execute a script, let’s turn our attention to PowerShell functions. A function is a block of code that can be called by name. Normally, a function exists within a script and is used whenever a particular block of code needs to be used more than once. Of course this isn’t the only way to set up a function. A function can be used outside of a script. Since functions normally reside in scripts however, let’s modify our script to include a function.

I want to try to keep things simple, so I really don’t want to add a big, complicated block of code to our script. Instead, let’s use some imagination. Let’s pretend that our script is actually much longer than it really is. Let’s also pretend that through the course of running the script, the value of $X changes several times and that we need to periodically check to see if the variable’s value is greater than zero.

In this type of situation, we could write an IF statement every time that the variable is assigned a new value, but that would result in a lot of repetitive code. I always think it is better to write less code whenever possible because shorter code blocks are less prone to coding errors.

So with that in mind, let’s place our IF statement inside a function. Since functions have to be named, I will call the function VarCheck.

To declare a function, all we have to do is enter the word Function, followed by the function name and the code block that we want to include within the function. The code block must be enclosed in braces {}.

So let’s place our IF statement into a function called VarCheck. The function would look something like this:

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

Any time that you want to call the function, you must simply enter the function’s name. If for example, we set the value of $X and then wanted to check to see if $X was greater than 0, we could use the following lines of code:

$X=4
VarCheck

The entire function, with the script looks like this:

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

$X=4
VarCheck

Obviously this is a really simple example, but it illustrates the way that a function works. If we wanted to reassign and recheck the value of $X a few times, we could do something like this:

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

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

You can see the output of this script in Figure D.

Image
Figure D: This script calls the VarCheck function several times.

Notice that each time we want to check the value of X, we are merely calling the function rather than retyping the comparison string. One thing to keep in mind is that even though my sample function consists of a single command, you can just as easily create functions that contain multiple commands.

Conclusion

I had originally intended for this to be the last article in my PowerShell essentials series, but I am going to write one more. The reason for this is that in the real world it is sometimes necessary to pass parameters to a function and I didn’t get a chance to talk about that. Therefore, I want to spend Part 7 talking more about functions.

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