PowerShell: Predefined Variables

By Xah Lee. Date: . Last updated: .

PowerShell has many 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, 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.

For a complete list of automatic variables, type

help about_automatic_variable

Variable names are case-insensitive

Environment Related Automatic Variables

Home Dir, Init File

home dir env var.
PS C:\Users\xah> $home
full path of PowerShell profile file. The profile is a init file.
PS C:\Users\xah> $profile
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

full path of the installation directory for Windows PowerShell.
PS C:\Users\xah> $pshome
C:\Program Files\WindowsApps\Microsoft.PowerShell_7.1.4.0_x64__8wekyb3d8bbwe
a object that represents the current host application. Sample value:
PS C:\Users\xah> $host

Name             : ConsoleHost
Version          : 7.1.4
InstanceId       :
UI               : System.Management.Automation.Internal.Host.InternalHostUserInterface
CurrentCulture   : en-US
CurrentUICulture : en-US
PrivateData      : Microsoft.PowerShell.ConsoleHost+ConsoleColorProxy
DebuggerEnabled  : True
IsRunspacePushed : False
Runspace         : System.Management.Automation.Runspaces.LocalRunspace
A hash table object containing info about your PowerShell version.
PS C:\Users\xah> $PsVersionTable

Name                           Value
----                           -----
PSVersion                      7.1.4
PSEdition                      Core
GitCommitId                    7.1.4
OS                             Microsoft Windows 10.0.19042
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.3
WSManStackVersion              3.0

Scripting Related Automatic Variables

Standard Values

.NET True. [see PowerShell: True/False]
The .NET False. [see PowerShell: True/False]
The .NET Null.

Arguments and Script Name

The current object in a pipeline. [see PowerShell: Pipe Output]
Get-ChildItem -recurse | where {$_.length -eq 0}
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.

help about_functions, help about_parameter

Returns the object that contains info of your script, function.
# get the script path

# get the script name

# look at what members this object has
$myinvocation | get-member
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.
Returns a path object that represents the full path of the current dir.

Iterators; Misc

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

help about_foreach help Foreach-Object

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

help about_comparison_operators


Returns True if last operation succeeded, else False.
Returns the exit code of the last program.
Returns a array of objects, representing the recent errors. $Error[0] is the most recent, $Error[1] is the error before that, etc.




Script Examples