EDUCBA Logo

EDUCBA

MENUMENU
  • Explore
    • EDUCBA Pro
    • PRO Bundles
    • Featured Skills
    • New & Trending
    • Fresh Entries
    • Finance
    • Data Science
    • Programming and Dev
    • Excel
    • Marketing
    • HR
    • PDP
    • VFX and Design
    • Project Management
    • Exam Prep
    • All Courses
  • Blog
  • Enterprise
  • Free Courses
  • Log in
  • Sign Up
Home Data Science Data Science Tutorials PowerShell Tutorial PowerShell Environment Variables
 

PowerShell Environment Variables

Priya Pedamkar
Article byPriya Pedamkar

Updated March 23, 2023

powershell-environment-variable

 

 

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.

Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

Set-Location Env:
Get-Item Env:

How to Declare PowerShell Environment Variables?

Below are the examples to declare PowerShell Environment Variables:

Example #1

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.

Code:

$env:ServerName = 'stage'
Get-ChildItem Env:ServerName

Output:’ stage’

$env:ServerName = 'production'
Get-ChildItem Env:ServerName

Output :’production’

Power Shell Environment Variables -1

Example #2

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.

Code:

$Env:<variable-name> += "<new-value>"
$Env:myFirst_ENV += "test1"
Get-ChildItem Env:/myFirst_ENV

Output:

Power Shell Environment Variables -2

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.

Get-ChildItem Env:

Output:

Power Shell Environment Variables -3

We can also display these details in sorting order to, look at the below example with the screen.

Get-ChildItem Env: | Sort-Object Name

Output:

Power Shell Environment Variables -4

Let’s talk about each one with the example, so if we want to see window path than we can write below command,

$env:WINDOWPATH

And if we want to see Path variable type details that we can write commands like

$env:PATH

See the below example and their outputs,

Output:

Power Shell Environment Variables - 5

We can see all  Temp folders look at the below commands

Get-ChildItem $Env:Temp -Recurse

Output:

Temp folders

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.

Code:

PS /home/ranjan> $env:CLUTTER_IM_MODULE
xim
PS /home/ranjan> $env:XDG_DATA_DIRS
/usr/share/ubuntu:/usr/local/share/:/usr/share/:/var/lib/snapd/desktop
PS /home/ranjan> $env:NODE_ENV
development
PS /home/ranjan> $env:DESKTOP_SESSION
ubuntu
PS /home/ranjan>

Output:

Power Shell Environment Variables - 7

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

Code:

$Env:FOO = "hello world"
Get-ChildItem FOO

Output:

Custom Environment

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,

Code:

Set-Variable -Name mycomputer -Value "ranjan-computer"
gv mycomputer -valueOnly

Output :ranjan-computer

Let us open another shell and log in to PWSH session and try to get “mycomputer “ variable name, like below,

pwsh
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,

Output:

local variables

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,

Get-ChildItem Env:NODE_ENV

Output:

global environment

global environment

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.

Conclusion

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.

Recommended Articles

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 –

  1. How To Install PowerShell
  2. Comparison Operators in PowerShell
  3. Examples to implement PowerShell Set-Location
  4. Top 16 parameters of PowerShell Get-ChildItem with properties
  5. PowerShell Sort-Object | Syntax | Parameters | Examples
  6. PowerShell Get-Item with Examples

Primary Sidebar

Footer

Follow us!
  • EDUCBA FacebookEDUCBA TwitterEDUCBA LinkedINEDUCBA Instagram
  • EDUCBA YoutubeEDUCBA CourseraEDUCBA Udemy
APPS
EDUCBA Android AppEDUCBA iOS App
Blog
  • Blog
  • Free Tutorials
  • About us
  • Contact us
  • Log in
Courses
  • Enterprise Solutions
  • Free Courses
  • Explore Programs
  • All Courses
  • All in One Bundles
  • Sign up
Email
  • [email protected]

ISO 10004:2018 & ISO 9001:2015 Certified

© 2025 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA Login

Forgot Password?

🚀 Limited Time Offer! - ENROLL NOW