Introduction to PowerShell Variables

When teaching PowerShell, there’s always a bit of a chicken and the egg situation. I want to teach PowerShell concepts, but sometimes I need to use something I haven’t had an opportunity to teach yet. One such item is the concept of variables in PowerShell. So let’s spend a little time looking at this from a beginner’s perspective so that you can move onto more interesting and useful PowerShell concepts.

What is a Variable?

At its simplest form, a variable is placeholder for something. That something could be a number or a string of text. It could be service object, or it could be bunch of service objects. The variable becomes a shortcut to this information. Typically, we create variables using the assignment operator, also known as the equal sign.

$n = "jeff"

The name of the variable can be as long or as short as you need it be. Technically you can use spaces, but I don’t recommend it. Stick with alphanumeric characters, and if you really, really need it, use the underscore or dash characters. You also don’t need to use prefixes like we did in the days of VBScript, often referred to as Hungarian Notation. There’s no need to define a variable like this:

$strComputername = "chi-core01"

When I see a command like this in a script, I can tell that the scripture hasn’t fully embraced the PowerShell paradigm. There’s no reason for the ‘str’ prefix because I already know that “Computername” is going to be a string.

Variable Naming Conventions

My rule of thumb is to use meaningful variable names like Computername or DriveType instead of X or Y. You don’t want to be on line 200 of your script looking at a line that references $X, and you can’t remember what $X is. Whereas, if you see $Computername, it’s pretty clear what you are using. In the interactive PowerShell console, feel free to use $X or abbreviated versions like $svc. In these ad-hoc situations, you know what you are typing.
There’s one final thing that I should point out regarding variable names. We often refer to them with the $ sign, as in “dollar sign computername.” Technically, the variable name is the text without the $. So in a script, if you see $Service, the variable name is actually ‘Service’. You only need to include the $ sign when referring to the variable.
You can also define a variable by assigning it the output of a command.

$running = get-service | where status -eq 'running'

Using Variables in Windows PowerShell

To use the variable, all you need to do is reference it in your command.

Write-Host "Hello, $name" -ForegroundColor magenta

Using a simple variable
Using a simple variable (Image Credit: Jeff Hicks)

Variables, with one exception, don’t have any properties or methods. They are whatever is inside of them. You can see that $n is a string:
Viewing a variable's type
Viewing a variable’s type (Image Credit: Jeff Hicks)

And $running is a service object.
Another variable type
Another variable type (Image Credit: Jeff Hicks)

And variables can contain anything, even a mix of objects should you really need it.

$stuff = 123,"PowerShell",(get-process -id $pid)

Defining a PowerShell variable with different items
Defining a PowerShell variable with different items (Image Credit: Jeff Hicks)

The variable, “stuff”, contains several different types of objects.
Viewing the different types
Viewing the different types (Image Credit: Jeff Hicks)

This is an odd use case, so we’ll drop it, but I wanted to show you what was possible.
Once you know what type of object is in your variable, you can use PowerShell to process it.
Using properties with the PowerShell variable
Using properties with the PowerShell variable (Image Credit: Jeff Hicks)

Remember I mentioned there was one variable property you could access? In some situations, you might be able to reference the Count property.
The count property
The count property (Image Credit: Jeff Hicks)

Although there’s no Count property for a Service object, PowerShell is smart enough to know you mean the underlying array or collection of service objects. Instead of Count you can usually use Length as well, although personally I never do.
One last point I want to make, as a general guideline, the variable is a point in time snapshot base on when you defined it. For example, when I defined $running, it contains all the running services at that time. I have 104. I can manually stop the BITS service, but the variable still shows it as running.
Variables don't automatically update
Variables don’t automatically update (Image Credit: Jeff Hicks)

In the case of a service object, it actually has a Refresh() method, which I can invoke on the variable.
Refreshing the service object variable
Refreshing the service object variable (Image Credit: Jeff Hicks)

I still have the same number of services, but now BITS has been update in the variable. I’m drifting a bit beyond beginner material but hopefully you’ll find this useful at a later date.

Stay Tuned for More on Variables

For beginners, what I’ve demonstrated in this article should meet most of your needs. But there are cmdlets you can use when working with variables and I want to touch on them in a separate article.