PowerShell Essentials (Part 5)

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

Throughout this article series, I have been working on answering some of the most fundamental questions pertaining to Windows PowerShell. For those who are just joining me, the questions are:

  • How can I figure out which cmdlet to Use?
  • How do I specify parameters for a cmdlet?
  • How can I get help with using a cmdlet?
  • How can I retrieve information about an object?
  • Why do some blocks of code not appear to be written in PowerShell?
  • How do PowerShell Scripts work?
  • Can I use variables?
  • How do PowerShell functions work?

In the previous article, I answered the question of why some blocks of code appear to be written in something other than PowerShell. In this article, I want to continue through my list of questions by explaining how to use variables.

If you have ever taken any sort of computer programming class before then you should be familiar with the concept of variables. A variable is a named container that stores a value of some kind. You can assign a variable a value directly, or a variable can store a calculated result.

Assigning a variable directly simply means telling PowerShell what value we want the variable to contain. Think back to math class for a moment. We all remember expressions such as X=4. X=4 is an example of directly assigning a value to a variable. We are assigning the value 4 to the variable X. Granted this isn’t PowerShell code, but I wanted to give you a simplified example.

A calculated result means that you can base a variable’s value on the result of a calculation. For example X=2+2 is a calculated variable assignment. We are still assigning X a value of 4 just as we did before, but this time we got there by performing a calculation.

OK, so now that I have talked a bit about variable assignments, let’s take a look at how PowerShell does things. There are two things that PowerShell does that makes it easy to work with variables.

First, you can use variables without first assigning them. I took several programming classes in school and many of the programming languages that I studied required variables to be declared within a block of code at the beginning of the program. PowerShell doesn’t have this requirement. You can use a variable on a whim.

The other thing that PowerShell does to make your life easier is to precede every variable with a dollar sign ($). This makes it really easy to spot variables.

So with that said, there are two primary methods for assigning a value to a variable in PowerShell. The first method involves using the Set-Variable cmdlet. To show you how this works, let’s pretend that I wanted to create a variable named $UserName and I wanted to assign it a value of Brien. I could accomplish this by entering the following command:

Set-Variable –Name UserName –Value “Brien”

As you look at this command, you will notice that it doesn’t make use of a dollar sign. The reason for this is simple. Because I used the Set-Variable cmdlet, PowerShell was smart enough to know that it needs to add a dollar sign in front of the variable name.

So what about retrieving the value of a variable? Well, that’s super simple. All you have to do is type the variable name. In this case, we could find the variable’s value simply by typing $UserName. You can see how this works in Figure A.

Image
Figure A: You can type a variable’s name to retrieve its value.

As previously mentioned, there is a second method that is commonly used to set a PowerShell variable. Remember when I used X=4 as an example of a direct variable assignment? Well, PowerShell is actually very similar. If I wanted to assign a value of 4 to a variable named X, I could do so by using the following command:

$X=4

You can see how this works in Figure B.

Image
Figure B: PowerShell allows you to assign a value to a variable without using the Set-Variable cmdlet.

Before I move on, I need to take just a second and talk about a couple of rules for variables. First, you will notice that I assigned two different data types. I showed you an example of assigning a string value (Brien) and an example of assigning an integer value (4). PowerShell is actually really flexible when it comes to variable assignments. Windows doesn’t seem to care if you are assigning text, numbers, or even blocks of code to a variable.

One thing that you do have to pay attention to however, is the variable name. There are certain words that you cannot use as a variable name because the words are reserved by PowerShell. These are words that you can use in PowerShell without using a cmdlet. For example, in the next article in the series I am going to show you how to create PowerShell functions. The word Function is reserved and cannot be used as a variable. The same also applies to words such as If, ForEach, and Return.

So how can you put a variable to work? As you will recall, I showed you some of the operators that are supported by PowerShell in one of the previous articles. These same operators can be used to compare the contents of a variable. This obviously isn’t the only way that you can use a variable, but it makes a good example.

Earlier, I set a variable called $X to a value of 4. Let’s do a really simple comparison to see if $X is greater than zero. If it is bigger than zero, we will type “Bigger than Zero” as a response. So here is the line of code that we will use:

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

At first glance, this looks like one of those really cryptic blocks of code that I talked about in the previous article, but there is a method to the madness. This line of code is essentially an if then statement.

There are three parts to the command. The first part is the word if. This tells PowerShell that we are doing an if then.

The second part of the command is ($X –GT 0). This is our comparison. $X is our variable. –GT is a PowerShell operator that means greater than. 0 is the value that we are comparing against. So in essence this part of the command is checking to see if the value within variable X$ is greater than zero.

The last part of the command is {“Bigger than Zero”}. This simply causes the text Bigger than Zero to be displayed if the condition in the second part of the command is true. Therefore the command could be summarized as if $X is greater than 0 then write Bigger than Zero. You can see the command in action in Figure C.

Image
Figure C: This command compares a variable against a static value.

Conclusion

In this article, I have explained how variables work within Windows PowerShell. In Part 6, I plan to conclude this article series by explaining how functions and scripts work within PowerShell.

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

Leave a Comment

Your email address will not be published.

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

Scroll to Top