
close
close
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.
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.
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'
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 (Image Credit: Jeff Hicks)
Viewing a variable’s type (Image Credit: Jeff Hicks)
Another variable type (Image Credit: Jeff Hicks)
$stuff = 123,"PowerShell",(get-process -id $pid)
Defining a PowerShell variable with different items (Image Credit: Jeff Hicks)
Viewing the different types (Image Credit: Jeff Hicks)
Using properties with the PowerShell variable (Image Credit: Jeff Hicks)
The count property (Image Credit: Jeff Hicks)
Variables don’t automatically update (Image Credit: Jeff Hicks)
Refreshing the service object variable (Image Credit: Jeff Hicks)
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.
More in PowerShell
Most popular on petri