Introduction to PowerShell Environment Variable
A variable is a block of memory that can be used to store values. PowerShell variables are denoted using the “$” symbol. A variable name can be anything from numbers, alphabets, or even underscores. Environment variable is a special type of variable that holds information related to the Operating system. It can be like a number of processors, location of temp folders, or the path of the operating system. It also contains information that is used by other programs and os. The windows-related information is stored in WINDIR environment variable. This article will explain in detail environment variables.
The syntax to fetch the value of an environment variable is as follows
$Env:<name of the variable>
Scope of Environment Variables:
There are three scope for an environment variable on windows. They are System scope, User scope and Process scope. As the name suggests system scope contains all the environment variables that are related to the computer, user scope contains variables that are related to a user and the process scope contains variables that are related to the current process.
To set an environment variable to a different value just type
$Env:<variable-name> = “new value to be set”
Where env represents environment and variable name should be the environment variable name. Changing an environment variable is applicable only within the current session. To make changes machine and user scope variables appropriate permission is needed.
To append a value to an existing variable, type the below
$Env:variablename += “;new value”
It adds the values specified to the existing value. The other ways of changing the value of an environment variable can be achieved using Set-Item, Copy-Item, or Remove-Item cmdlets.
To change the values of variables in machine or user scope, system. Environment class’s methods should be made use of.
PowerShell can store user preferences in environment variables. These are like preference variables but are inherited. Some examples of preference variable include
It contains the current session’s execution policy set as its value. This exists only when it is set for a single session. This can be achieved in two ways; by running the Set-ExecutionPolicy cmdlet and setting the scope parameter value to a process. The second is by starting a session from command line.
PowerShell check for obsolete modules to avoid unnecessary cache when working with module analysis cache. This can be turned off by setting its value to 1.
This contains a list of locations that contain modules and resources that are being searched. System-related locations are stored in $PSHOME\Modules. The windows related management modules are installed here. User-installed modules specify whether the module installed is specified for one user or for everyone. The two locations are $env:ProgramFiles\PowerShell\Modules and /usr/local/share/PowerShell/Modules.
Common Environment Variables:
Some of the commonly available environment variables are as follows
- SessionName- Used for identifying the current session
- SystemRoot and Windir- Path to current installation
- UserName- Name of currently logged in user
- USerProfile- Location of user profile
Managing Environment Variables:
There are four different methods that are available for managing environment variables.
- The Item cmdlets
- The Environment provider drive
- On Windows, the System Control Panel
- The .NET System.Environment class
Using Item Cmdlets:
We can use the Get-ChildItem cmdlet to get the value of an environment variable. Since the environment variables do not have child items, Get-Item and Get-ChildItem gives the same result. The output can also be sorted. We can also copy the value of environment variables Copy-Item cmdlet. To remove an environment variable, Remove-Item and clear-Item cmdlets can be used. Variables can also be removed using Rename-Item cmdlet.
Write-Host "Managing environment variables using Get Cmdltes" -ForegroundColor Green
Write-Host "Fetching the computers name" -ForegroundColor Green
Get-ChildItem Env: Computername
Write-Host "Display all environment variables" -ForegroundColor Green
Write-Host "Sorting the environment variables by name" -ForegroundColor Green
Get-ChildItem Env: | Sort Name
Using Environment Provider:
All the environment variables are represented by an instance in the System.Collections.DictionaryEntry class. It is stored as a key-value pair. Dictionary key is the name of the variable and value is the value of the variable. To get the properties and methods of an environment variable Get-Member cmdlet can be used. To set the location Set-Content cmdlet can be used.
Write-Host "Managing environment variables using environment provider" -ForegroundColor Green
Write-Host "Fetching all the env variables" -ForegroundColor Green
Get-Item -Path Env:* | Get-Member
Write-Host "Setting the env variable" -ForegroundColor Green
Set-Content -Path Test -Value 'Testvalue'
From Control Panel:
Environment variable value can be changed from control panel as well. We need to select advanced system settings and on the advanced tab select environment variable. We add new variables or modify the existing ones in the system and user scope. Automatically these values are added to the registry by windows. This can also be performed at the PowerShell profile as well.
Using System.Environment Methods:
There are two methods inside the environment class that helps in updating or modifying an environment variable’s value. They are GetEnvironmentVariable and SetEnvironmentVariable.
Write-Host "Demo of setting variable using .net methods" -ForegroundColor Green
$tp = [Environment]::GetEnvironmentVariable('PSModulePath', 'Machine')
Write-Host "Setting a environment variable" -ForegroundColor Green
Write-Host "value is" -ForegroundColor Green
Thus, the article explained in detail about environment variable in PowerShell. It explained the various scopes of environment variables, their usage, and declaration. It also showed how an environment variable can be deleted, renamed, or appended to a new value. It walked us through various examples in which environment variables can be set and changed. To learn in detail, it is advisable to write sample scripts and practice them.
This is a guide to PowerShell Environment variable. Here we discuss Introduction, syntax, and examples. You may also have a look at the following articles to learn more –