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

---

ATA Learning

Tap to hide

[

ATA Learning

](/)

*   [Home](/)
*   [Tutorials](/tutorials/)
*   [Instructors](/author/)
*   [Advertising](/advertising/)
*   [Recommended Resources](/resources/)
*   [About Adam](/about-adam/)

Search for:  

*   [](https://twitter.com/adbertram)
*   [](https://github.com/Adam-the-Automator)
*   [](https://www.linkedin.com/company/adam-the-automator-llc)
*   [](/feed/)

![PowerShell Automatic Variables: Special Variables Built into PowerShell](https://adamtheautomator.com/wp-content/uploads/2024/11/image-51.png)

# PowerShell Automatic Variables: Special Variables Built into PowerShell

[![](https://secure.gravatar.com/avatar/d0b9d42e21e5622713f8b693aa5c0f9244d5f7dd200ed29b8398f52dee5de337?s=192&d=mm&r=g)Adam Bertram](https://adamtheautomator.com/author/adam-bertram/)25 November 20242 min. read

Categories: [IT Ops](/category/it-ops/)

Tags:[PowerShell](/tag/powershell/)

Table of Contents

*   [Modifying Automatic Variables](#modifying-automatic-variables)
*   [Creating and Managing Constants](#creating-and-managing-constants)
*   [Checking Exit Codes](#checking-exit-codes)
*   [Defining and Using Null Variables](#defining-and-using-null-variables)

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](https://adamtheautomator.com/wp-content/uploads/2024/11/image-52.png)

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](https://adamtheautomator.com/wp-content/uploads/2024/11/image-53.png)

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](https://adamtheautomator.com/wp-content/uploads/2024/11/image-54.png)

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

![](https://adamtheautomator.com/wp-content/uploads/2024/11/image-55.png)

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)](https://adamtheautomator.com/wp-content/uploads/2024/11/image-56.png)

#### 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](https://adamtheautomator.com/wp-content/uploads/2024/11/image-57.png)

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

```
$foo = 'something'
$foo
```

![Defining and calling a variable](https://adamtheautomator.com/wp-content/uploads/2024/11/image-58.png)

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](https://adamtheautomator.com/wp-content/uploads/2024/11/image-59.png)

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

Share this article

[Share on X](https://twitter.com/intent/tweet?url=https%3A%2F%2Fadamtheautomator.com%2Fpowershell-automatic-variables%2F&text=PowerShell%20Automatic%20Variables%3A%20Special%20Variables%20Built%20into%20PowerShell)[Share on Facebook](https://www.facebook.com/sharer/sharer.php?u=https%3A%2F%2Fadamtheautomator.com%2Fpowershell-automatic-variables%2F)[Share on LinkedIn](https://www.linkedin.com/sharing/share-offsite/?url=https%3A%2F%2Fadamtheautomator.com%2Fpowershell-automatic-variables%2F)

## Related Posts

![](https://adamtheautomator.com/wp-content/uploads/2026/06/26995-troubleshoot-dns-issues-powershell-codex.webp)

### [Troubleshoot DNS Issues with PowerShell](/troubleshoot-dns-issues-powershell/)

Troubleshoot DNS issues with PowerShell by testing name resolution, DNS client settings, cache entries, and network connectivity in a repeatable workflow.

![](https://adamtheautomator.com/wp-content/uploads/2025/10/image_2025-10-24_095322075.png)

### [Migrating from PowerShell 6 to 7.5: Breaking Changes/New Features](/migrating-powershell-6-to-7-5/)

Migrate from PowerShell Core 6 to 7.5: breaking changes, features, and testing tips.

![](https://adamtheautomator.com/wp-content/uploads/2025/06/featured-image-6.png)

### [How to Add Timeouts to Pester Tests with PowerShell Runspaces](/pester-test-timeout-runspaces/)

Prevent Pester tests from hanging indefinitely using PowerShell runspaces. Learn to handle variable scoping, module loading, TestDrive access, and stream capture challenges with timeout protection.

## Categories

*   [IT Ops](/category/it-ops/)
*   [Cloud](/category/cloud/)
*   [DevOps](/category/devops/)
*   [Home Ops](/category/home-ops/)
*   [Information Security](/category/infosec/)
*   [Software Development](/category/software-development/)

## Site

*   [Home](/)
*   [Tutorials](/tutorials/)
*   [Instructors](/author/)
*   [Advertising](/advertising/)
*   [Recommended Resources](/resources/)
*   [About Adam](/about-adam/)

Copyright 2026© ATA Learning | [Privacy Policy](/privacy/)
