Updated March 23, 2023
Introduction to PowerShell Environment Variables
Environment Variables contain operating system information, like the path of the operating system, processor count used by your operating system and address of your system temporary folders. The first question here is, why we need environment variables? let me tell you one example, suppose you have one script, and this script has to work differently according to the environment like if the script is running on your local computers it has to perform differently and if it is running a server computer it has to work differently. Now the question is how the script will know where it is running? So the answer is very simple, with the help of environment variables it can differentiate and perform tasks accordingly. Our operating system contains few environment variables, for example when we log in to our computer system it set our home directories. We will see it’s syntax and examples. To see PowerShell Environment variables use the below command.
How to Declare PowerShell Environment Variables?
Below are the examples to declare PowerShell Environment Variables:
In the below example we are assigning an environment variable for the setting environment, and With command Get-ChildItem we are fetching environment name. Suppose you want to set database different for a different environment than we can set the environment variable name and according to environment name we can set database names.
$env:ServerName = 'stage'
$env:ServerName = 'production'
We can define our own environment variable and also we can fetch its value on need. In the below example we can see a very simple syntax and its example. We are setting the value of myFirst_ENV and with the command Get-ChildItem command, we are fetching its value.
$Env:<variable-name> += "<new-value>"
$Env:myFirst_ENV += "test1"
List of PowerShell Environment Variables
There are two types of environment variables they are:
1. System Defined
System Defined available one system defined, for example where your computers start it knows how to set home directories and how to set other important paths,
There are many environments in any system, like environment related to the window, environment-related to the path and environment related to processors length. below command will display all available environments. It values are different for a different architect of the system like for Window, for Linux and for apple systems.
We can also display these details in sorting order to, look at the below example with the screen.
Get-ChildItem Env: | Sort-Object Name
Let’s talk about each one with the example, so if we want to see window path than we can write below command,
And if we want to see Path variable type details that we can write commands like
See the below example and their outputs,
We can see all Temp folders look at the below commands
Get-ChildItem $Env:Temp -Recurse
To see any environment variable value we can use the command below along with the screen, simply we are passing the name of the environment variable which we wanted to see and it will display.
PS /home/ranjan> $env:CLUTTER_IM_MODULE
PS /home/ranjan> $env:XDG_DATA_DIRS
PS /home/ranjan> $env:NODE_ENV
PS /home/ranjan> $env:DESKTOP_SESSION
2. Custom Environment
Another Environment variables are custom environment variable these are variables defined by for our own uses . for example if we have developed an application and we want it to run in different ways on the different environment like
$Env:FOO = "hello world"
Advantages of Using Power Shell Environment Variables
You have created many variables in Powershell, but have you ever realize that those variables are local, which means they can be available on a current session only, once you start another PWSH session these variables will not be available for you .lets see below examples.
Let us create a local variable variable “mycomputer” on the current session,
Set-Variable -Name mycomputer -Value "ranjan-computer"
gv mycomputer -valueOnly
Let us open another shell and log in to PWSH session and try to get “mycomputer “ variable name, like below,
gv mycomputer -valueOnly
Output: gv: Cannot find a variable with the name ‘mycomputer’.
Hence this variable is not available for other sessions and it through an error. Environment variables are special variables as they are available for you on all new shells.
See the screen of example for local variables,
Let’s take the example of environment variable, here we have one environment variable called NODE_ENV and we are getting its value development on one shell and once we open a new shell and tried to find it’s value we shaw it was returning same which is “development”, that’s mean environment variables are available globally.
See the examples for global environment variables,
From the above example, we understand that environment variables are available for use in every new shell.
You may be able to understand the biggest and main advantage of using environment variables in PowerShell. It simply helps us to recognize the system environment at run time which allows our scripts to perform different tasks according to the values of the environment.
Main Advantages Are,
- Value of environment variables are persistence, that’s mean available in all terminals.
- If we have large groups of servers then it will be very easy to manage them all by defining environment variables.
- If you want to manage your application in a better way and do not want to touch internal code for configuration, then we can use environment variables.
- You will have some better security because most of the hackers are trying to hack and look after codes and applications to them, they can not be able to manipulate and access environment variables.
- If you want to set a quick test environment and do not want to give access of a whole lot of code to nontechnical users then we can set environment variables and give access to different users according to our environment set by us for them.
PowerShell Environment variable, environment variables are very useful at a time when we are trying to run application and want to use a variable globally that means that variable should be available for all the running process and script.
This is a guide to PowerShell Environment Variables. Here we discuss examples to declare, two types of environment variables, advantages and an advantages example. You can also go through our other related articles to learn more –