PowerShell Automatic Variables: Special Variables Built into PowerShell

Published:25 November 2024 - 2 min. read

In this lesson, we’ll explore automatic variables in PowerShell – special variables that are built into the system. Unlike variables we create ourselves, these automatic variables are predefined by PowerShell and serve specific purposes. Let’s look at some common examples and how to use them.

Modifying Automatic Variables

Let’s look at $MaximumHistoryCount, for example. This automatic variable defines how many lines of your command history are saved.

$MaximumHistoryCount

💡 Tip: Run the Get-History command sometimes if you’re curious about what commands you’ve executed previously.

Verifying the maximum history count

The maximum value looks like it’s set to 4096 lines.

Let’s change the value to 200 because I don’t need that much history.

$MaximumHistoryCount = 200

Perfect! But, most automatic variables cannot be overwritten.

Let’s try to change the value of $PSEdition.

$PSEdition = 'something else'
Attempting to change the $PSEdition variable’s value

That attempt didn’t work because the majority of automatic variables are read-only or defined as constants.

Creating and Managing Constants

Another aspect of managing variables is setting them as constants, which means once you set them, you can’t change them.

To create a constant, use the Set-Variable cmdlet and the Option parameter.

Set-Variable -Name 'color' -Value 'green' -Option Constant
$color
$color = 'blue'

It looks like the same error message. It’s likely that the $PSEdition automatic variable is defined as a constant.

Attempting to override a constant variable’s value

Checking Exit Codes

Here’s another useful automatic variable when working with executables: the $LastExitCode variable. When an executable ends, it returns an exit code that indicates the result, such as 0 for success, 1 for failure, etc.

Using the $LastExitCode variable, you can run an executable and then check the exit code of the last result.

Let’s demonstrate by pinging some unknown host and checking the value of $LastExitCode.

ping.exe 111.111.111.111 -n 1
$LastExitCode

Notice the exit code is 1, which is expected since the ping didn’t succeed.

Now, let’s ping something to get a successful result.

ping.exe 127.0.0.1 -n 1; $LastExitCode

Notice the returned value is now 0. Also, look at how two actions were performed on the same line. You can do this in PowerShell by using the semicolon.

PowerShell, by default, thinks each line you type is a separate execution, so it treats a semicolon like a new line and executes actions in sequence.

Running a successful ping and checking the exit code (0)

Defining and Using Null Variables

Let’s end with one more automatic variable you’ll probably use a lot: the $null variable. This variable is a special one that is a value and isn’t at the same time.

This variable is simply a placeholder for nothing, particularly useful in strict mode.

Set-StrictMode -Version Latest

Strict mode gets picky with variables. You can’t just try to call a variable, like $foo; it will return an error.

$foo
Calling a variable while in strict mode

But if you define the variable, it’s fine.

$foo = 'something'
$foo
Defining and calling a variable

But what if you plan to define a variable but not have a value? Enter the $null variable.

Assign your variable to the $null variable; you now have a defined variable with no value.

$bar = $null
Get-Variable -Name bar
Defining a null variable

That’s it for variables. We covered all of the basics, and hopefully, there are some hints on where you can explore further.

Hate ads? Want to support the writer? Get many of our tutorials packaged as an ATA Guidebook.

Explore ATA Guidebooks

Looks like you're offline!