Home Β» PowerShell Β» PowerShell Built in Variables

PowerShell Built in Variables

PowerShell has built-in variables that are a set of predefined variables available for use in scripts. These variables provide access to important information such as current environment settings, system properties, and script execution details.

PowerShell has automation variables, common built-in variables, special variables, and event variables.

In this article, we will discuss the various types of built-in variables available in PowerShell and their usage in scripts.

Common Built-in Variable

Some commonly used built-in variables in PowerShell include:

$ or $PSItem – Represents the current object in the pipeline, loop, or filters

The following script uses $_ to print the name of each file in the current directory.

Get-ChildItem | ForEach-Object { Write-Host $_.Name }

$HOME – Represents the user’s home directory location

The following script uses the $HOME variable to change the working directory to the users home directory

Set-Location $HOME

$PWD – Represents the current working directory

The following script uses the $PWD to get the list of files in the current working directory using the Get-ChildItem cmdlet.

Get-ChildItem $PWD

$NULL – Represents the null value.

The following script assigns the null value to a variable

$filePath = $NULL

PowerShell Automatic Variables

Automatic variables are created and managed by PowerShell. They store information about the current execution environment.

Below is the list of automatic variables in PowerShell.

$Args – Contains the arguments passed to function or script

The following script uses the $Args to print the arguments passed to a script:

param($arg1, $arg2)
Write-Host "Arguments: $arg1, $arg2"

In the above PowerShell script, $arg1 and $arg2 are defined using the param statement. The Write-Host cmdlet is used to print the values of these two arguments to the console. Save the script as an example-script.ps1

Run the above script with arguments as given below:

.\example-script.ps1 "shell" "admin"

The output of the above PowerShell script is:

Arguments: shell, admin

$Error – Holds an array of errors that occur during the script execution

The following script uses the $Error automatic variable to display the error that occurs in the current PowerShell session.

Get-ChildItem -Path 'D:\PS\FRVTemp\'
$Error[-1].Exception.Message

In the above PowerShell script, the Get-ChildItem cmdlet is used to get items from the directory specified which doesn’t exist, which will generate an error.

The $Error variable is used to access the last error object in the array and .Exception.Message property is used to display the error message.

$ExecutionHost – Provide access to the current execution context

The following script uses that $ExecutionHost to get the session state object information.

$ExecutionContext.SessionState

$Host – Contains the information about the current host application

The following script uses the $HOST automatic variable to get the current version of the PowerShell host.

$HOST.Version

The output of the above PowerShell script returns the PowerShell host version:

PS D:\> $HOST.Version

Major  Minor  Build  Revision
-----  -----  -----  --------
5      1      18362  2212


PS D:\>

$Input – Stores the input passes to a function or script through the pipeline

The following PowerShell script uses the $Input script to count the number of items passes through the pipeline.

$Input | Measure-Object | Select-Object -ExpandProperty Count

$MyInvocation – Provide details about the current script or command invocation

The following script uses the $MyInvocation variable to get the name of the current script.

$MyInvocation.MyCommand.Name

$PROFILE – Represents the path to the current user’s profile script.

The following script uses the $PROFILE variable to access the current user’s profile script.

notepad $PROFILE

In the above PowerShell script, the $PROFILE variable gets the current user’s profile script and Notepad is used to open the script in a text editor.

$PSBoundParameters – Contains a dictionary of params passed to a script or function

The following script uses the $PSBoundParameters to print the values of the parameters that were passed to the function.

function Example-Function {
    $PSBoundParameters
}

Example-Function -Param1 "Shell" -Param2 "Admin"

The above PowerShell script creates a function β€œExample-Functionβ€œ. Run the script and save it as an example-script.ps1 file.

Open the PowerShell console, and run the script. $PSBoundParameters variable will print parameter values.

$PSCommonPath – Stores the path to the current script or command

The following script uses the $PSCommandPath to get the path to the current script.

$PSCommandPath

$PSHome – Refers to the directory where the PowerShell is installed

The following script uses the $PSHome automatic variable to get the PowerShell installation directory path and join the path using the Join-Path cmdlet with pwsh.exe

Join-Path $PSHome "pwsh.exe"

$PSScriptRoot – Represents the directory containing the current script

The following script uses the $PSScriptRoot automatic variable to get the path to the directory containing the current script.

$PSScriptRoot 

Cool Tip: How to write variables to file in PowerShell!

PowerShell Special Variables

PowerShell provides special variables that have special constant values.

  • $true – Represents the boolean true value.
  • $false – Represents the boolean false value.
  • $null – Represents the null value.

PowerShell Event Variables

PowerShell provides event variables that are used while working with PowerShell events.

  • $Event – Refers to the current event object.
  • $EventArgs – Represents the event arguments of the current event.
  • $Sender – Refers to the object that raised the current event.

Conclusion

I hope the above article on PowerShell built-in variables and how to utilize them in a scripting environment is helpful to you.

You can find more topics about PowerShell Active Directory commands and PowerShell basics on the ShellGeek home page.