Environment Variables store information related to the current environment, like the Operating System and user sessions like our current terminal. They are global variables, meaning we can access them across commands and programs. The operating system usually creates them, but we can also use them to configure our production environment.
List Environment Variables
In PowerShell, environment variables are stored as strings. We can use the Get-ChildItem
cmdlet on the Env:
drive to get a complete list of all existing environment variables.
PS > Get-ChildItem Env: Name Value ---- ----- BROWSERSLIST_IGNORE_OLD_DATA 1 EIN_IMAGE ubuntu …
To get the specific value of an environment variable, we can use either of the following commands:
(Get-Childitem Env:EIN_IMAGE).Value > ubuntu $Env:EIN_IMAGE > ubuntu
Two popular environment variables in PowerShell are HOME
and PATH
. The HOME
environment variable specifies the current user’s home directory, whereas PATH
includes all the directories where applications look for executables.
Create an Environment Variable
The syntax to create environment variables in PowerShell is as follows:
$Env:EXAMPLE_ENV_VAR = "custom value"
By convention, environment variable names are usually capitalized.
The benefits of environment variables are that they are accessible across the terminal session and scripts. This allows our session to operate in one environment where all data is consistent across processes.
Instructions
List all the environment variables using the PowerShell Terminal.
Click Run to check your work.
Create a new environment variable called USERS
and assign your name as the value. Be sure to use all capital letters.
If you list all the environment variables again, you will now see USERS
in the list.
Click Run to check your work.
Print the value of the environment variable USERS
.
Click Run to check your work.
Run the environment_variables.ps1
script file in the terminal by typing ./environment_variables.ps1
.
Click Run to check your work.