Quantcast
Channel: Windows Networking - TechGenix
Viewing all articles
Browse latest Browse all 130

PowerShell power lesson: This is no ordinary variable

$
0
0

If you follow my work on this site, then you know that I write a whole lot of PowerShell-related content. One of the expressions that I tend to use in my PowerShell scripts on a regular basis is dollar sign underscore dot, (or $_.). Recently, I had someone ask me what those characters meant, and I realized that I have never really taken the time to explain the significance of this series of characters, or why they are used so frequently. So I want to take the opportunity to do that now.

The interesting thing about $_. is that the first of these three characters is a dollar sign. In PowerShell, placing a dollar sign in front of a word (or a series of characters) denotes that the word is a variable. If for example, I wanted to assign my name to a variable, I might use a command like this:


$Name = ‘Brien’


Similarly, PowerShell allows you to map an entire command string to a variable. I might, for instance, create a variable called $Processes and set it equal to the Get-Process cmdlet, as shown below:


$Processes = Get-Process


Why $_. is no ordinary variable

The point is that the dollar sign in $_. indicates that $_. is a variable. However, this is no ordinary variable. Instead, it is often referred to as a variable in the pipeline.

In PowerShell, the word pipeline generally refers to a series of commands that have been joined together. Individual commands are separated from one another by using the pipe symbol (hence the name pipeline). When commands are joined together in a pipeline, the output from one command is used as input for the next command in the sequence.

To give you a more concrete example, consider the Get-Process cmdlet. This cmdlet retrieves a list of the processes that are running on the machine. You can then use the pipe symbol to treat the list of processes as the input for the next command in the series. You might, for example, want to narrow down the list to look at some specific processes. To do so, you could pipe the Get-Process command’s output into the Where-Object cmdlet, which can filter that output. Here is an example.


Get-Process | Where-Object {$_.ProcessName -eq ‘dllhost’}


This command shows all of the processes whose name is equal to dllhost. You can see the command and its output shown in the image below.

$_.

You will notice that the command that I am using here includes the variable in the pipeline. Before I begin dissecting the command and explaining how it and the variable in the pipeline work, there is one thing that I want to clarify. Although $_. Is commonly referred to as a pipeline variable, there is nothing stopping you from using a normal variable in a pipeline. PowerShell is extremely flexible when it comes to the ways in which it allows variables to be used.

If you look back at the previous screen capture, you will notice that the information that is presented within the output is divided into a series of columns and that each of those columns has a name. The Where-Object portion of the command is looking at a specific column (ProcessName) and is checking to see if it is equal (-EQ) to a particular value (dllhost). So where does the pipeline variable come into play?

The pipeline variable essentially acts as a shortcut. Earlier I mentioned that it is possible to assign a command to a variable. If you look at the image below, you can see that I have mapped the Get-Process command to a variable called $Processes. If I were to simply type the variable name, PowerShell would generate the same output as if I manually executed the Get-Process cmdlet, which you can also see in the figure.

$_.
Now, if I were to type the variable name, followed by a period and a column name, PowerShell would output the contents of that column. You can see what this looks like in the next image.

$_.
So with that in mind, consider our original command:


Get-Process | Where-Object {$_.ProcessName -eq ‘dllhost’}


In this command, our pipeline variable also uses a period and a column name. In fact, the underscore character is essentially just taking the place of a variable name, thereby saving us the trouble of declaring a variable.

So if that is true, then we should be able to observe similar functionality from the variable that we have already declared ($Processes). Since we set the $Processes variable to be equal to the Get-Process cmdlet, we can replace the Get-Process portion of the command above with the variable name. The commands would, therefore, look like this:


$Processes=Get-Process
$Processes | Where-Object {$_.ProcessName -eq ‘dllhost’}


You can see the output below.

$_.
So in this command the $_. variable acts as a shortcut. We reference the $Processes variable once and then use $_. throughout the rest of the command rather than having to type out the full variable name. But just to show you that the pipeline variable truly is just a shortcut for referencing the variable by name, check out the screen capture below. Here, I have gotten an output that is very similar to that of my original command by referencing $Processes.ProcessName.
$_.

Take the shortcut

As you can see, $_. is essentially just a shortcut that prevents you from having to retype a variable name within a pipeline command. Even so, it is worth noting that a pipeline variable and a full expression of the variable are not interchangeable. If you were to use:


$Processes | Where-Object {$Processes.ProcessName -eq ‘dllhost’}


Instead of:


$Processes | Where-Object {$_.ProcessName -eq ‘dllhost’}


The output would not be filtered as requested.

The post PowerShell power lesson: This is no ordinary variable appeared first on TechGenix.


Viewing all articles
Browse latest Browse all 130

Trending Articles