Taking Your PowerShell Variables to the Next Level

Image of a lit Hola sign on a brick wall.
When you reach the next level in PowerShell you find this easter egg!
SOURCE: RawPixel

Declaring a variable in PowerShell is usually a simple matter. Just specify the variable name, an equal sign, and the value you want to assign to the variable. Then PowerShell will do the rest. Despite this simplicity, you can make working with variables easier with a few extra tricks. For instance, you can define constants and use variables as items

In this article, I’ll share 3 of my favorite variable-related techniques, including renaming variables, showing all variables, and working with constants. But first, I’ll explain the concept of variables as items.

Variables as Items

Before I start, I need to explain the concept of variables as an item. Usually, when you hear items discussed with regard to PowerShell, those items are files. The Get-ChildItem cmdlet, for example, lists the contents of the current folder within the file system, similar to the old DIR command. 

However, files are just one of many different items that PowerShell supports. Variables are another supported item type. If you want to use an item-related cmdlet to reference a variable, you need to set the Path parameter to Variables. I’ll show you a few examples of this as I go along.

3 Next-Level PowerShell Variable Uses

1. Renaming Variables

If you’re writing a complex PowerShell script and leveraging external components, you may occasionally discover that you need to rename one or more variables. You can easily accomplish this using the Rename-Item cmdlet.

To show you how this works, suppose that you have a variable named $A, and you need to change its name to $B. To do so, you would use this command:

Rename-Item -Path Variable:A -NewName B

As previously mentioned, you must set the path to a variable. When you do this, notice the colon and A now appended to the word variable. “A” refers to the variable you’re renaming. Microsoft strangely decided not to use the dollar sign when referring to variables. You use the NewName parameter to specify the variable’s new name—in this case, B.

Screenshot of the $A variable being used in PowerShell console.
The variable $A is being renamed to $B.

2. Showing All Variables

Earlier in this article, I explained that you could direct item-related cmdlets toward variables. I also mentioned that you commonly use the Get-ChildItem cmdlet to display files in a folder. 

However, if you can use item-related cmdlets with variables, then it stands to reason that the Get-ChildItem cmdlet should be able to return a list of variables just as it can return a list of files.

If you use an item cmdlet to reference a variable, you must set the Path parameter to a variable. As such, if you want to see a list of all the variables currently in use, use this command:

Get-ChildItem -Path Variable:

Screenshot of the Get-ChildItem cmdlet can be used to retrieve a list of all variables and their values.
The Get-ChildItem cmdlet can be used to retrieve a list of all variables and their values.

3. Working with Constants

Looking at the figure above, you may find that the output differs from what you expected. After all, I’ve only declared a single variable, $A, which I later renamed to $B, yet a whole list of values appears.

I want to point out a few things. In the previous screen capture, you can see a variable with the name B. This is the variable I declared earlier. Remember, Item cmdlets do not use the dollar sign when working with variables, so $B is expressed as B.

The output also includes user-defined variables, system variables, constants, environment variables, and more. If you want to see what types of variables appear within the output, change the command to:

Get-ChildItem -Path Variable: | Select-Object Name, Value, Options

Screenshot of the Options column after it's been added to the output to display variable type.
The options column has been added to the output to display variable type.

The fact that PowerShell lists some variables as constants in the output shown above implies that you can create constants in PowerShell. The trick is to create a new variable and set the variable’s Option to Constant

Let’s assume we want to create a constant named $C and set its value to 1. In a situation like this, we’ll have to use the New-Variable cmdlet instead of just typing $C=1. Here is what the command looks like:

New-Variable -Name C -Value 1 -Option Constant

As shown in the figure below, once we create a constant, it becomes read-only and cannot be changed.

Screenshot of the read-only error message in the PowerShell console.
Once defined, you can’t modify a constant.

Final Thoughts

Although treating a variable as an item might seem odd, PowerShell has no problem doing this. Generally speaking, you can direct any item-related cmdlet you use with the file system toward variables, and you need to set the cmdlet’s path to a variable. In this article, I discussed how to rename variables, show all variables, and work with constants in PowerShell. Use these nifty tricks the next time you want to take your PowerShell variable use to the next level!

Learn more about PowerShell and similar topics in the FAQ and Resources sections below!

FAQ

Can you rename a variable without treating it as an item?

Yes, in a roundabout way, you can rename a variable through other means. Set the new variable equal to the old variable (such as $B = $A) and then delete the original variable. You can remove the original variable using the Remove-Variable cmdlet or set it to a $Null value.

Is it better to use the New-Variable cmdlet to create a variable instead of just setting the variable name equal to a value?

No rule says that you have to use the New-Variable cmdlet. However, the New-Variable cmdlet can make your PowerShell code easier to read. Some tasks, such as defining a constant, can only be accomplished using the New-Variable cmdlet.

Yes, you can. The cmdlet that you would use is Set-Item. Again, you would need to set the path equal to the variable. You would also have to specify the Value parameter and the variable’s new value. If you wanted to assign a new value to $A, you might type: Set-Item -Path Variable:A -Value 2

Since I can use the Remove-Item cmdlet to delete files, can I use it to delete variables?

As long as you haven’t configured a variable as a constant, you can delete it using the Remove-Item cmdlet. Additionally, any constant you create remains during the PowerShell session. 

What is the difference between a read-only variable and a constant?

You can’t change a read-only variable, but you can remove it with the Remove-Item cmdlet so long as you use the -Force parameter. However, the -Force parameter will not remove a constant, and a constant remains for the session duration.

Resources

TechGenix: Article on PowerShell Global Variables

Learn how PowerShell global variables can improve productivity.

TechGenix: Article on Securing PowerShell to Stop Bad Actors

Discover how you can stop bad actors using your PowerShell to launch attacks.

TechGenix: Article on Using PowerShell Core on Linux Machines

Learn how to use PowerShell Core on Linux virtual machines.

TechGenix: Article on PowerShell Scripts for Release Azure DevOps

Find out how to create a custom PowerShell script for release retention in Azure DevOps.

TechGenix: Article on PowerShell Exchange Online Cmdlets

Discover how to resolve PowerShell Exchange Online cmdlet failures.

About The Author

Leave a Comment

Your email address will not be published. Required fields are marked *

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

Scroll to Top