When I first started learning about PowerShell many years ago, one of the concepts that I had trouble wrapping my head around was that of mapping PowerShell commands to variables. In case you have never encountered this before, PowerShell makes it possible to link a variable to a command so that when you call the variable, the command is executed. While there is nothing overly difficult about mapping PowerShell commands to a variable, it can sometimes be tricky to get the variable to give you the output that you might expect. As such, I thought that I would share with you a few tricks that I have picked up over the years.
PowerShell allows you to map nearly any command to a variable, but for the sake of demonstration, I want to pick a single command and stick with it for all of the examples that I will be providing throughout this article. The cmdlet that I will be using is Get-PhysicalDisk.
When you enter the Get-PhysicalDisk cmdlet directly into PowerShell, you will receive an output that looks something like what you see in the image below.
You can assign a command to a variable in the same way that you would assign a constant value to a variable. Just enter the variable name, followed by an equal sign, and the command. As you can see in the figure below, this doesn’t generate any visible output, but calling the variable directly (by entering the variable name at the command line) causes the associated command to execute.
Of course, variables are often used for comparative purposes, so what happens if we need to examine one particular physical disk attribute for the sake of making a comparison? Well, as is so often the case with PowerShell, there are several ways of doing this. The easiest way to look at a particular attribute is to call the variable name, and then append a period, followed by the name of the attribute. For example, if you mapped the Get-PhysicalDisk command to a variable named $A and you wanted to check the disk’s health status, then you could enter this command:
As an alternative, you could append the Select-Object command to the variable name. The problem with using Select-Object is that doing so returns header information as well. If you needed to make a comparison, then the header information would get in the way. Let me show you what I mean. The lines of code assign the exact same piece of information to variables $A and $B, but $B uses the Select-Object method, which results in header information being included. If I compare the two variables, then I get a status of False, indicating that their values are different from one another, as shown in the image below.
$A = Get-PhysicalDisk
$B = Get-PhysicalDisk | Select-Object HealthStatus
$A.HealthStatus -eq $B
One thing that you might have noticed about the way that I have been using the $Get-PhysicalDisk cmdlet is that this particular computer only has one physical disk. What if we had two physical disks, and wanted to make some comparisons between them? I realize that you may never need to use PowerShell to compare the disks in your computer but remember that I am only using the Get-PhysicalDisk cmdlet as an example. The techniques that I am discussing work with a wide variety of PowerShell cmdlets.
To show you how this works, I am going to switch to a different computer. When I enter the Get-PhysicalDisk cmdlet on this system, it shows that this computer is equipped with two hard disks, an HDD, and an SSD. So what happens if we map a variable to the Get-PhysicalDisk cmdlet and then attempt to look at the HealthStatus parameter in exactly the same way that we did on a computer with only one physical disk? As you can see in the figure below, we see an indication that both disks are healthy.
Now suppose that I am only interested in the health of the SSD. There are a few different ways that I could filter the output, but here is one way of doing it:
$A=Get-PhysicalDisk | Where MediaType -eq ‘SSD’
In actuality, this block of code would show me the health status of every SSD in the system. In this case, though, I only have one. If I had multiple SSDs and needed to narrow down the list, I could filter by the disk’s friendly name or serial number.
Now that I have shown you how to look at a specific disk, let’s do a disk comparison. My HDD is obviously far larger than my SSD, but let’s use PowerShell to prove it, while also mapping each individual disk to a variable. Here are the commands that I will be using:
$SSD = Get-PhysicalDisk | Where MediaType -eq ‘SSD’
$HDD = Get-PhysicalDisk | Where MediaType -eq ‘HDD’
$SSD.Size -GT $HDD.Size
As you look at the commands above, you will notice that I have switched things up a bit with regard to the variable names. Rather than using variable names like $A or $B, I have used $SSD to represent the SSD drive, and $HDD to represent the HDD drive. As such, the first two lines of code are just variable assignments. The last line of code checks to see if the size of the SSD drive is bigger (-GT) than the size of the HDD drive. Because the SSD drive is smaller than the HDD drive, this command should return an output of false. If I reverse the command and check to see if the HDD is bigger than the SSD, then the output should flip to True. You can see an example of this in the image below.
Mapping PowerShell commands: An essential skill
Although mapping PowerShell commands to variables can take a bit of getting used to, it is an essential skill for those who plan to do any sort of advanced PowerShell scripting. By mapping commands to variables, a variable can come to represent an object such as a disk or a virtual machine, thereby making it easy for the script to reference that object.
Featured image: Pexels
More PowerShell Basics articles
- PowerShell Gallery: A great free resource for finding scripts
- Using JSON with PowerShell: A guide for IT professionals
- Reading text files with PowerShell and Linux bash shell
- How malicious PowerShell scripts evade detection
- Building PowerShell hash tables