Powershell

What Does $_ Mean in PowerShell

PowerShell has a long list of cmdlets and functions that can perform various tasks. Apart from this, PowerShell does support various variables such as “$PSVersionTable“, “” “$_“.

The “$_” is said to be the pipeline variable in PowerShell. The “$_” variable is an alias to PowerShell’s automatic variable named “$PSItem“. It has multiple use cases such as filtering an item or referring to any specific object.

We have compiled a brief guide on the “$_” in PowerShell with the following learning outcomes:

  • How $_ works in PowerShell
  • How to use $_ in PowerShell

How $_ works in PowerShell

PowerShell commands may produce a multiple-page output where you need only specific entities. In such a situation, the “$_” variable produces the required results. Let’s have a look at the following line that shows how “$_” is used with other cmdlets in PowerShell.

Cmdlet | Where/Where-Object/ForEach-Object {$_.<field> -flag <value>}

The <field> represents the column/field name and the <value> is any entity of the <field>. The “$_” is used to retrieve only specific values from <field> under the flag condition. Moreover, the “.” used after “$_” is used to get the names of the available fields on the screen.

Note: Usually, the “Match” or “eq” flag is used to return only specific entities.

How to use $_ in PowerShell

The “$_” variable is used by administrators to get specific results. This section lists various use cases of “$_” in PowerShell.

Note: Here, we have used the PowerShell ISE to show the usage of the “$_” as the PowerShell ISE provides extensive support for using “$_“. However, it can be implemented in PowerShell as well.

How to use $_ with the Where clause in PowerShell

The primary purpose of the “$_” is to get the selected content from any cmdlet. The following code is used to filter the result of the “Get-Command” cmdlet in PowerShell:

Get-Command | Where {$_.CommandType -eq 'Alias'}

The “$_” is used in the “Where” clause to display only those entities where the “CommandType” is equal to “Alias“.

Note: We have saved the script as “mean.ps1“.

Now, execute the script in the terminal window as follows:

./mean.ps1

It is observed from the output that only “Alias” entities are printed on the window.

How to use the $_ with the Where-Object in PowerShell

The Where-Object cmdlet performs a specific operation on each item of the collection. The following code pipes the “Where-Object” with the “Get-Process” cmdlet to filter specific processes using the “$_” variable.

Get-Process | Where-Object {$_.ProcessName -match 'chrome'}

The $_ operator has only retrieved the processes that have ProcessName equal to “chrome

It can be observed that only “chrome” processes are printed on the screen.

Conclusion

The $_ is a variable or also referred to as an operator in PowerShell that is used to retrieve only specific values from the field. It is piped with various cmdlets and used in the “Where” , “Where-Object“, and “ForEach-Object” clauses of the PowerShell. This article provides a detailed overview of the “$_” variable in PowerShell alongside its working and usage in various scenarios.

About the author

Adnan Shabbir