How to List All Environment Variables in PowerShell?

In this tutorial, I will explain how to list all environment variables in PowerShell. Environment variables are useful for configuring the operating system and applications. They store data such as system paths, user profiles, and other configuration settings.

What Are Environment Variables?

Environment variables are dynamic values that can affect the way processes run on a computer. They are used by the operating system and applications to store configuration data. For instance, the PATH variable contains a list of directories where executable files are located.

Listing All Environment Variables using PowerShell

In PowerShell, environment variables are accessed through the Env: drive. You can use various cmdlets to list and manage these variables.

Using Get-ChildItem to List Environment Variables

The Get-ChildItem cmdlet (alias gci) is commonly used to list all environment variables. Here’s how you can do it:

Get-ChildItem Env:

This command will display all environment variables and their values in the current session. For a more readable output, you can format the list:

Get-ChildItem Env: | Format-Table -AutoSize

I executed this cmdlet in my local system, and it displays me the list of environment variables like in the screenshot below:

Listing All Environment Variables using PowerShell

Example: Listing Environment Variables on a Server

Let’s say you are managing a server for a tech company in New York. You need to check all environment variables to troubleshoot an application issue. Here’s how you can list them:

PS C:\> Get-ChildItem Env: | Format-Table -AutoSize

Name                           Value
----                           -----
ALLUSERSPROFILE                C:\ProgramData
APPDATA                        C:\Users\JohnDoe\AppData\Roaming
CommonProgramFiles             C:\Program Files\Common Files
COMPUTERNAME                   NY-SERVER-01
HOMEDRIVE                      C:
HOMEPATH                       \Users\JohnDoe
LOCALAPPDATA                   C:\Users\JohnDoe\AppData\Local
LOGONSERVER                    \\NY-DC-01
PATH                           C:\Windows\system32;C:\Windows;C:\Program Files\PowerShell\7
PSModulePath                   C:\Users\JohnDoe\Documents\PowerShell\Modules;C:\Program Files\PowerShell\Modules
TEMP                           C:\Users\JohnDoe\AppData\Local\Temp
USERDOMAIN                     NY-TECH
USERNAME                       JohnDoe
USERPROFILE                    C:\Users\JohnDoe

Check out Get the Windows Version Using PowerShell

Filter Specific Environment Variables

Sometimes, you may only need to view a specific environment variable. You can filter the output using the Where-Object cmdlet:

Get-ChildItem Env: | Where-Object { $_.Name -eq 'PATH' }

This command will display only the PATH variable and its value.

Here is an example.

Imagine you are a developer in San Francisco working on a project that requires specific tools in the PATH. You can check if they are included:

PS C:\> Get-ChildItem Env: | Where-Object { $_.Name -eq 'PATH' }

Name                           Value
----                           -----
PATH                           C:\Windows\system32;C:\Windows;C:\Program Files\Git\cmd;C:\Program Files\nodejs

Manage Environment Variables

In addition to listing environment variables, PowerShell allows you to create, modify, and remove them.

Add a New Environment Variable

To add a new environment variable, use the New-Item PowerShell cmdlet:

New-Item -Path Env: -Name "MY_NEW_VARIABLE" -Value "MyValue"

Example

Suppose you are setting up a new project and need to add a custom environment variable:

PS C:\> New-Item -Path Env: -Name "LA_PROJECT_PATH" -Value "C:\Projects\LA_Project"

Check out Rename a Computer Using PowerShell

Modify an Existing Environment Variable

To modify an existing environment variable in PowerShell, use the Set-Item cmdlet:

Set-Item -Path Env:\MY_NEW_VARIABLE -Value "NewValue"

Example

Imagine you need to change the TEMP directory for a server in Chicago:

PS C:\> Set-Item -Path Env:\TEMP -Value "D:\Temp"

Remove an Environment Variable

To remove an environment variable in PowerShell, use the Remove-Item cmdlet:

Remove-Item -Path Env:\MY_NEW_VARIABLE

Example

If you are cleaning up temporary variables on a server in Miami, you can remove them as follows:

PS C:\> Remove-Item -Path Env:\TEMP_VARIABLE

Conclusion

In this tutorial, I explained how to list environment variables in PowerShell using the Get-ChildItem Env: cmdlet and how to manage environment variables, such as adding, modifying, and removing an environment variable. I hope this helps.

You may also like:

100 PowerShell cmdlets download free

100 POWERSHELL CMDLETS E-BOOK

FREE Download an eBook that contains 100 PowerShell cmdlets with complete script and examples.