CodeSteps

Python, C, C++, C#, PowerShell, Android, Visual C++, Java ...

PowerShell – Automatic Variables – $$, $^, $? and $_

Maintaining state information is very important in any of the Applications to enable to understand the behavior of the flow of execution. For example, it is required to know whether the previous command was succeeded and based on that you will write the scripts for the execution.

PowerShell maintains the state information in a set of variables called automatic variables; prefixed with dollar sign (“$”). Usually the state variables or automatic variables are for read-only.

In this Article, we will discuss about few of the Automatic Variables in PowerShell.

Get last token in the command – The $$ automatic variable

$$  – This variable contains the last executed command. If the command has pipeline, it contains the last command in the pipeline. For example, your last command is clear; this variable contains the value clear. If the last command in the pipeline is Sort-Object, it contains the last command. Here is the working example:

PS C:\> $$
clear
PS C:\> Get-ChildItem | Sort-Object
PS C:\> $$
Sort-Object

Then how do we get the first command in the pipeline? This is what $^ variable do it for us.

Get first token in the command – The $^ automatic variable

$^ – The automatic variable $$ gives the last executed command. Similarly, $^ also gives the last executed command. But if the command has pipeline, it gives the first command in the pipeline; whereas above variable gives the last command in the pipeline. With the below example, you can see the difference:

PS C:\> ls
PS C:\> $^
ls
PS C:\> Get-Process | Sort-Object
PS C:\> $^
Get-Process

Get last command status – The $? automatic variable

$? – This is an important automatic variable; which tells whether the previous command is successfully executed or not. If the previous command is Success, it contains the value True otherwise it contains the value False. Let’s look at this below examples:

PS C:\> Get-Service
PS C:\> $?
True
PS C:\> Code-Steps
Code-Steps : The term 'Code-Steps' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:1
+ Code-Steps
+ ~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (Code-Steps:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

PS C:\> $?
False

Observe that, when the non existing command has given; PowerShell throws the ObjectNotFound exception. And also the state of it saved into $? variable.

Get current object – The $_ automatic variable (or $PSItem)

Another important automatic variable PowerShell provides is $_. This contains the current object in the execution. Using this you can perform specific action on every object or on selected objects. Below command shows the usage of it; which displays the Process with the Id 1144.

PS C:\> Get-Process | Where-Object {$_.Id -EQ 1144}

Handles  NPM(K)    PM(K)      WS(K) VM(M)   CPU(s)     Id ProcessName
-------  ------    -----      ----- -----   ------     -- -----------
   5922      69    46068      55268   262   135.63   1144 svchost

This variable is same as $PSItem variable.

There are so many automatic variables in PowerShell. We will discuss them as we go.

[..] David

PowerShell – Automatic Variables – $$, $^, $? and $_

3 thoughts on “PowerShell – Automatic Variables – $$, $^, $? and $_

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to top