---
title: PowerShell Automatic Variables: Special Variables Built into PowerShell
description: Learn about PowerShell's automatic variables - built-in special variables that serve specific purposes. Discover how to work with history limits, constants, exit codes, and null values.
canonical: https://adamtheautomator.com/powershell-automatic-variables/
---

# PowerShell Automatic Variables: Special Variables Built into PowerShell

> Learn about PowerShell's automatic variables - built-in special variables that serve specific purposes. Discover how to work with history limits, constants, exit codes, and null values.

Source: https://adamtheautomator.com/powershell-automatic-variables/

---

- PowerShell Automatic Variables: Special Variables Built into PowerShell Tap to hide Home

- Tutorials

- Guidebooks

- Instructors

- Get Paid to Write

- Advertising

- Recommended Resources

- About Adam

                Search for:

-

-

-

-

# PowerShell Automatic Variables: Special Variables Built into PowerShell

        Published:25 November 2024 - 2 min. read

- PowerShell

          [](https://adamtheautomator.com/author/adam-bertram/)

            [Adam Bertram](https://adamtheautomator.com/author/adam-bertram/)

            Read [more tutorials](https://adamtheautomator.com/author/adam-bertram/) by Adam Bertram!

-

-

        [](https://specopssoft.com/product/specops-password-auditor/?utm_source=adamtheautomator&utm_medium=referral&utm_campaign=adamtheautomator_referral_na&utm_content=display)
        Audit Active Directory for stale users, weak passwords, and other security risks with [Specops Password Auditor](https://specopssoft.com/product/specops-password-auditor/?utm_source=adamtheautomator&utm_medium=referral&utm_campaign=adamtheautomator_referral_na&utm_content=text).

Table of Contents

- Modifying Automatic Variables
- Creating and Managing Constants
- Checking Exit Codes
- Defining and Using Null Variables

                XFacebookLinkedIn

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.

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'

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.

#### 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.

#### 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

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

$foo = 'something'
$foo

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

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](https://adamtheautomator.com/ata-guidebooks/)

## More from ATA Learning and Partners

- ### Recommended Resources! Recommended Resources for Training, Information Security, Automation, and more!

- ### Get Paid to Write! ATA Learning is always seeking instructors of all experience levels. Regardless if you’re a junior admin or system architect, you have something to share. Why not write on a platform with an existing audience and share your knowledge with the world?

- ### ATA Learning Guidebooks ATA Learning is known for its high-quality written tutorials in the form of blog posts. Support ATA Learning with ATA Guidebook PDF eBooks available offline and with no ads!

## Categories

- IT Ops

- Cloud

- DevOps

- Home Ops

- Information Security

- Software Development

## Site

- Home

- Tutorials

- Guidebooks

- Instructors

- Get Paid to Write

- Advertising

- Recommended Resources

- About Adam

        Copyright 2026&copy; ATA Learning | [Privacy Policy](https://adamtheautomator.com/privacy/)

                                                        Don't be left behind with the ATA Learning Newsletter!

Looks like you're offline!
