PowerShell: Automatic Variables
PowerShell has many builtin variables, called automatic variables.
💡 TIP: Variable names are case-insensitive
Environment Related
Home Dir, Init File
$home-
- full path of the user's home directory.
$home # C:\Users\xah $profile-
full path of Profile (init file).
PowerShell Info
$PsHome-
full path of the installation directory for PowerShell.
$PsHome # C:\Program Files\PowerShell\7 $host-
a object that represents the current host application. Sample value:
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
$PsVersionTable-
A hash table object containing info about your PowerShell version.
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 SerializationVersion 1.1.0.1 WSManStackVersion 3.0
Scripting Related
Standard Values
$true-
represent true.
$false-
represent false.
$null-
- Represent no value.
- Its the default value for variable.
Arguments and Script Name
$_-
The current object in a pipeline.
dir | ForEach-Object {Write-Host $_ }
$MyInvocation-
value is the object that contains info of your script, function.
# 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-
value is 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-
value is a path object that represents the full path of the current dir.
Iterators
$ForEach-
value is a enumerator object of the current ForEach-Object loop. This var exists only when a “for loop” is running.
Regex
$Matches
Error
$?-
value is True if last operation succeeded, else False.
$LastExitCode-
value is the exit code of the last program.
$Error-
value is a array of objects, representing the recent errors.
$Error[0]is the most recent,$Error[1]is the error before that, etc.
Complete List of Automatic Variables
type help about_automatic_variable
$$$?$^$_$args$ConsoleFileName$Error$Event$EventArgs$EventSubscriber$ExecutionContext$false$foreach$HOME$Host$input$IsCoreCLR$IsLinux$IsMacOS$IsWindows$LASTEXITCODE$Matches$MyInvocation$NestedPromptLevel$null$PID$PROFILE$PSBoundParameters$PSCmdlet$PSCommandPath$PSCulture$PSDebugContext$PSHOME$PSItem$PSScriptRoot$PSSenderInfo$PSUICulture$PSVersionTable$PWD$Sender$ShellId$StackTrace$switch$this$true
PowerShell, variable and assignment
sigils war, magic chars in variable name
- Variable Naming: English Words Considered Harmful
- Parameter names start with phi φ, variable names start with xi ξ
- The Sigil War, Syntactic Indicator for Types of Function and Variable (2016)
- Elisp: DOLLAR SIGN $ and AT SIGN @ in Variable Name
- Jargon: Predicate in Programing Languages (2014)
- Syntactic Meaning of Variable (2018)
- Perl: Variable Name Prefix (aka Sigil)
- Ruby: Variable Name Conventions
- PowerShell: Automatic Variables
- Clojure: Variable Name Conventions