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:
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:
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:
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:
Example 2: Get All the User’s List Using the “Get-Item” Cmdlet
Now, get the list of users on Windows:
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:
Scope 2: Machine
The machine scope contains the environment variables that are associated with the running instances of Windows:
Scope 3: Process
Environment variables in the process scope combine user and machine scopes:
That is all about using the PowerShell environment variables.
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.