Understanding Automatic Variables in Powershell

In PowerShell, one has the ability to call a variable just about any name in the book. The only exceptions to this are the variables that are already being managed by PSH, known as the Automatic Variables. These variables, without a doubt, will be the most repetitious objects you use in PSH next to functions. In this tutorial, you will learn about automatic variables in PowerShell.

Setup

If you have not already done so, click open Windows PowerShell ISE.
Step one.

In order to get a list of all the automatic variables, run the following command:

Example

get-help about_automatic_variables
    

Copy and Try it

The list is actually quite long to show the output pane's contents, so we provide a chart of automatic variables for your convenience below:

Variable Name

Description

$$

Contains the last token in the last line received by the shell.

$?

Contains the state of the last operation. True when successful, false otherwise.

$^

Contains the first token in the last line received by the shell.

$_

Contains the current object in the pipeline object.

$Args

Contains the current object in the pipeline object.

$ConsoleFileName

Stores the filename of the most recently reported console file.

$PSCulture

Contains the current culture used by the OS.

$Error

An array of error objects representing the most recent errors.

$ExecutionContext

Contains the EngineIntristics objects that represents the execution context of the Windows PowerShell host.

$False

It contains the value False.

$ForEach

Contains the enumerator of a foreach-object loop.

$Home

Stores the full path to the user's home directory.

$Host

Current host application for Windows PowerShell.

$Input

Contains the object currently in the pipeline in the Process block of a function.

$LastExitCode

Contains the exit code of the last Windows program executed.

$MyInvocation

Contains information about the current command. Useful for dynamically retrieving the filename and path of the current script.

$NestedPromptLevel

Stores the current prompt level for nested prompts. This is a bit advanced, so you may want to run the get-help about_automatic_variables command for additional information

$NULL

Contains NULL or empty value.

$PID

Contains the process identifier of the Windows PowerShell process.

$Profile

Stores the full path to the Windows PowerShell user profile for the default shell.

$PSHome

Stores the full path to the installation of Windows PowerShell.

$PSVersionTable

A hash table containing details about the version and build of the current Windows PowerShell console.

$Pwd

Stores the full path to the current directory.

$ShellID

Contains the identifier for the current shell.

$True

Contains TRUE

$PSUICulture

Stores the name of the UI culture currently in use.

Step two.

There are quite a few variables created and managed upon execution of PowerShell. You can use TRUE and FALSE that actually contains the appropriate values rather than creating empty strings and as for values of the variables, you can query the values for the variables such as $PSVersionTable hash table and get all the information you need.

Remarks last but not least…

These variables are made to make our lives easier when scripting in PowerShell. Join us next time for additional Windows PowerShell tutorials! Till then…