Powershell

PowerShell Environment Variables: The Ultimate Guide

Environment variables in PowerShell are used to access or launch specific paths, folders, or files in Windows. Using environment variables can help to access a specific folder easily. Instead of typing the full path address to access the “Temp” folder, the “%temp%” environment variable can be used.

Moreover, these variables can be considered as placeholders or shortcuts to the long path address of files or folders. It is all about making navigation easy on a computer, whether it is adding paths to applications or creating custom scripts.

This blog will provide details about using the environment variables.

What are PowerShell Environment Variables?

These are the commands that are going to be discussed:

Method 1: Use the “dir env:” Cmdlet to Get the Environment Variables

The “dir env:” cmdlet is specially designed to get the environment variables using PowerShell. Execute the stated command to get all the environment variables:

dir env:

Method 2: Use the “Get-ChildItem -Path Env:” Cmdlet to Get the Environment Variables

The “Get-ChildItem” cmdlet is used along the “-Path” parameter and “Env:” variable to get the environment variables on Windows:

Get-ChildItem -Path Env:

According to the above code:

  • The “Get-ChildItem” cmdlet is used to get the item from specified directories.
  • -Path” parameter is used to define the path.
  • Env:” variable returns environment variables:

Method 3: Use the “Get-Item -Path Env:” Cmdlet to Get the Environment Variables

Similarly, the environment variables can also be retrieved using the “Get-Item” cmdlet along with the “-path” parameter and “Env:” variable:

Get-Item -Path Env:

Here, the specified “Get-Item” cmdlet is used to get the item from a specified location but does not get the file’s content:

Example 1: Get the Current Username Using the “Get-Item” Cmdlet

This example will use the “Get-Item” cmdlet to get the current username of Windows:

Get-Item -Path Env:USERNAME

Example 2: Get All the User’s List Using the “Get-Item” Cmdlet

Now, get the list of users on Windows:

Get-Item -Path Env:user*

Environment Variable Types

There are three types of environment variables scopes: user, machine, and process. When all these environment variables scope combine, they form a picture that provides various environment variables to the running processes.

Scope 1: User

User scope contains the environment variables associated with the user running the current process. To get the user scope environment variables to execute the below command:

[System.Environment]::GetEnvironmentVariables('User')

Scope 2: Machine

The machine scope contains the environment variables that are associated with the running instances of Windows:

[System.Environment]::GetEnvironmentVariables('Machine')

Scope 3: Process

Environment variables in the process scope combine user and machine scopes:

[System.Environment]::GetEnvironmentVariables('Process')

That is all about using the PowerShell environment variables.

Conclusion

Environment variables can be considered as placeholders or shortcuts to access files or folders that have long path addresses. These can be retrieved in PowerShell using several cmdlets, including “dir env:”, “Get-ChildItem -Path Env:”, or “Get-Item -Path Env:”. This blog has presented in-depth detail about the PowerShell environment variables.

About the author

Muhammad Farhan

I am a Computer Science graduate and now a technical writer who loves to provide the easiest solutions to the most difficult problems related to Windows, Linux, and Web designing. My love for Computer Science emerges every day because of its ease in our everyday life.