V 0.1
PowerShell 7.0 - Quick Reference
www.practicalpowershell.com
Automatic Variables
(not exhaustive)
Variables that store state information, created/maintained by
PowerShell and should be treated as Read-Only.
$$ Last token in the last line received by the
session
$? Contains the execution status of the last
command.
$^ Contains the first token in the last line received
by the session.
$_,$PSItem Current object in the pipeline object.
$args Contains an array of values for undeclared
parameters that are passed to a function, script,
or script block.
$ConsoleFileName Contains the path of the console file (.psc1)
that was most recently used in the session.
$Error Array of errors from previous commands.
$ExecutionContext Contains an EngineIntrinsics object that
represents the execution context of the
PowerShell host.
$foreach Contains the enumerator of a ForEach loop.
$HOME Full path of the user's home directory.
$Host Represents the current host application for
PowerShell.
$input Enumerates all input passed to a function.
$IsCoreCLR .NET Core Runtime check. $True/$False
$IsLinux $True if Operating system is Linux.
$IsMacOS $True if Operating system is Mac.
$IsWindows $True if Operating system is Windows.
$LastExitCode Exit code of the last Windows-based program
that was run.
$Matches Hash table of any string values matched with
the -match and -notmatch operators.
$MyInvocation Contains information about the current
command, such as the name, parameters,
parameter values, and more.
$null Represents an empty or null value.
$PID Process identifier (PID) of PowerShell session.
$PROFILE Full path of the PowerShell profile for the
current user and the current host application.
$PSCulture Reflects the culture of the current session.
$PSDebugContext This variable contains information about the
debugging environment.
$PSHome Full path of the installation directory for
PowerShell
$ConfirmPreference Determines whether PowerShell
automatically prompts you for
confirmation before running a
cmdlet or function.
$DebugPreference Determines how PowerShell
responds to debugging.
$ErrorActionPreference Determines how PowerShell
responds to a non-terminating error.
$ErrorView Determines the display format of
error messages in PowerShell.
$FormatEnumerationLimit Determines how many enumerated
items are included in a display.
$InformationPreference Lets you set information stream
preferences that you want displayed
to users.
$MaximumHistoryCount Determines how many commands
are saved in the command history
for the current session.
$OFS The Output Field Separator specifies
the character that separates the
elements of an array that is
converted to a string. Default (“ “)
$OutputEncoding Determines the character encoding
method that PowerShell uses when it
sends text to other applications.
$ProgressPreference Determines how PowerShell
responds to progress updates.
$PSEmailServer Specifies the default e-mail server
that is used to send email messages.
$PSSessionConfigurationName Specifies the default session
configuration that is used for
PSSessions created in the current
session.
$PSSessionOption Establishes the default values for
advanced user options in a remote
session.
$VerbosePreference Determines how PowerShell
responds to verbose messages
generated.
$WarningPreference Determines how PowerShell
responds to warning messages
generated.
$WhatIfPreference Determines whether WhatIf is
automatically enabled for every
command that supports it.
Preference Variables
$PSItem, $_. Contains the current object in the pipeline
object.
$PSScriptRoot Directory from which a script is being run.
$PSSenderInfo Contains the directory from which a script is
being run.
$PSUICulture Name of the user interface (UI) culture for OS.
$PSVersionTable Read-only hash table that displays details about
the version of PowerShell that is running in the
current session.
$PWD Path Object - full path of the current directory.
$ShellID Identifier of the current shell.
$StackTrace Stack trace for the most recent error.
$Switch Contains the enumerator not the resulting
values of a Switch statement.
https://docs.microsoft.com/en-us/powershell/module/
microsoft.powershell.core/about/about_automatic_variables
https://docs.microsoft.com/en-us/powershell/module/
microsoft.powershell.core/about/about_preference_variables
Variables
Examples: Change value of variable
$Path = ‘C:\Scripts\TestScript’ $Path = ‘C:\Windows\System32’
$Date = Get-Date $Date = ($Date).AddDays(-90)
$Processes = Get-Process $Processes = (Get-Process).Name
Clear Variable of values
Clear-Variable -Name $Path
Clear-Variable -Name $Date
Clear-Variable -Name $Processes
Scoped
$Global:Server=’Ex01' Global variable, visible everywhere
$Local:Count=1 Visible in local scope and child scopes
$Private:State=’Test’ Visible in local scope, but not child scopes
Multi-Assignment
$State,$Count,$PC = ‘Enabled’, ‘1', ‘Windows10’
Flip Variables
$Count1=3 ; $Count2=5 ; $Count1,$Count2 = $Count2,$Count1
Read-Only Variable (can be overwritten with -Force)
Set-Variable 'PermRef' -Value '1973' -Option ReadOnly
Constant Variable Cannot be overwritten
Set-Variable 'Important' -Value '1973' -Option Constant
Variable Acceptable Values:
[ValidateRange(90,150)][int]$Tolerance = 99
$Tolerance = 151 #Returns error – not valid for the variable