PowerShell Predefined Variables

By Xah Lee. Date: . Last updated: .

PowerShell has several predefined variables, called automatic variables. Some of them are used for scripting, some give info about the environment. For example, $_ is a variable that contains the current argument (similar to Perl's “$_”), and is a most frequently used variable in scripting. The $Host is a variable that contains info about current PowerShell environment.

This page gives explanation and sample use, of the most frequently used automatic variables. The page is divided into 2 sections. One section on environment related variables, and the other section is about scripting related variables.

For a complete list of automatic variables, type help about_automatic_variable.

Environment Related Automatic Variables

Home Dir, Init File

$Home

Evaluates to the home dir env var, equivalent to %homedrive%%homepath% env var. Sample output:

C:\Users\xah

$Profile

Eval to the full path of your PowerShell profile file. The profile is a init file. Sample output:

C:\Users\xah\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1

Note that even if this file does not exist, it still returns the path. To check if the file exists, use: test-path $profile.

PowerShell Info

$PsHome

Evaluates to the full path of the installation directory for Windows PowerShell. Sample outputs:

C:\Windows\system32\WindowsPowerShell\v1.0\
C:\Windows\SysWOW64\WindowsPowerShell\v1.0\

Note that even if you have PowerShell version 2, the dir path is still “v1.0”, because PowerShell 2 is meant to be compatible with version 1.

$Host

Eval to a object that represents the current host application. Sample output:

Name             : Windows PowerShell ISE Host
Version          : 2.0
InstanceId       : b18c3b59-d4c3-4373-9ce3-3f54768bb3e4
UI               : System.Management.Automation.Internal.Host.InternalHostUserInterface
CurrentCulture   : en-US
CurrentUICulture : en-US
PrivateData      :
IsRunspacePushed : False
Runspace         : System.Management.Automation.Runspaces.LocalRunspace

If you are running PowerShell in Windows Console or emacs, the name line may be:

Name             : ConsoleHost

$PsVersionTable

Eval to a hash table object containing info about your PowerShell version.

Sample output:

Name                           Value
----                           -----
CLRVersion                     2.0.50727.4016
BuildVersion                   6.1.6949.0
PSVersion                      2.0
PSCompatibleVersions           {1.0, 2.0}

Scripting Related Automatic Variables

Standard Values

$True

Eval to .NET “True”.

Whenever you need boolean, use $true or $false.

If you simply type “true” in the command line, it's a error, because PowerShell tries to interprete it as a command or expression. By design, “true” or “false” are not recognized as expressions.

PowerShell True/False Interpretation
ValueValue in Boolean Context
$trueTrue
Nonzero numberTrue
Nonempty stringTrue
Nonempty arrayTrue
Hashtable (empty or not)True
$falseFalse
$nullFalse
ZeroFalse
Empty stringFalse
Empty arrayFalse

$False

Evaluates to the .NET “False”.

$NULL

Eval to the .NET “Null”.

Arguments and Script Name

$_

The current object in a pipeline.

Note that a pipe can pass multiple objects, and each one is processed in turn. The “$_” is the current one.

Example use:

# get all aliases of get-childitem
get-alias | where-object {$_.Definition -eq "get-childitem"}

In the above, the “get-alias” returns many objects. We use the syntax “xyz.Definition” to refer to the “Definition” property of the object xyz. In this case, we use “$_” to refer to the current object, instead of a object name such as “xyz”.

$Args

A array of the argument received by your function or script or block of code.

Note, PowerShell provide a few ways to define how a function receives its arguments. A function can be defined with explicit parameters, or it can be defined without explicit parameters. When a function does not have explicit parameters, it can still receive arguments, and these arguments can be accessed by $Args.

get-help about_functions, get-help about_parameter

$MyInvocation

Returns the object that contains info of your script, function. Example usage:

# get the script path
$myinvocation.mycommand.path

# get the script name
$myinvocation.mycommand.name

# look at what members this object has
$myinvocation | get-member

$Input

Returns a enumerator object that contains the input that is passed to a function. The items in the enumerator are the objects in the current pipeline.

$Pwd

Returns a path object that represents the full path of the current dir.

Iterators; Misc

$ForEach

Returns a enumerator object of the current ForEach-Object loop. This var exists only when a “for loop” is running.

get-help about_foreach get-help Foreach-Object

$Matches

Returns a hash table that represents matched text, from using the “-match” operator.

about_comparison_operators

Error

$?

Returns True if last operation succeeded, else False.

$LastExitCode

Returns the exit code of the last program.

$Error

Returns a array of objects, representing the recent errors. $Error[0] is the most recent, $Error[1] is the error before that, etc.

If you have a question, put $5 at patreon and message me.

PowerShell

  1. Install
  2. Help Command
  3. PowerShell as cmd.exe
  4. list Alias, find Alias
  5. Piping Output and Input
  6. Environment Variables
  7. Predefined Variables
  8. Creating PowerShell Scripts
  9. PowerShell vs Bash