EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 360+ Courses All in One Bundle
  • Login
Home Data Science Data Science Tutorials PowerShell Tutorial PowerShell Environment Variables
Secondary Sidebar
PowerShell Tutorial
  • Variables
    • PowerShell Variables
    • PowerShell Environment Variables
    • PowerShell set environment variable
    • Hashtable in PowerShell
    • Set Variable in PowerShell
  • Basics
    • PowerShell Restart Service
    • PowerShell comment
    • PowerShell Map Network Drive
    • PowerShell Append to File
    • PowerShell print
    • What is PowerShell
    • Uses Of Powershell
    • PowerShell Empire
    • PowerShell Parameter
    • PowerShell Stop Service
    • PowerShell Versions
    • How To Install PowerShell
    • PowerShell uninstall module
    • How to Use PowerShell?
    • PowerShell Logging
    • PowerShell Tools
    • PowerShell Commands
    • PowerShell Version Command
    • PowerShell Administrator
    • PowerShell Modules
    • PowerShell Registry
    • PowerShell block Comment
    • PowerShell Verbs
    • PowerShell list
    • PowerShell add user to group
    • PowerShell Write to Console
    • Variable in PowerShell
    • PowerShell New Line
    • PowerShell prompt for input
    • PowerShell File Extension
    • Powershell Remotesigned
    • PowerShell Write to File
    • PowerShell Ping
    • PowerShell wget
    • PowerShell Global variable
    • PowerShell Get-ADGroup
    • Array in PowerShell
    • PowerShell Multidimensional Array
    • PowerShell Array of Strings
    • PowerShell? join array
    • Useful PowerShell Scripts
    • String in PowerShell
    • PowerShell Switch Statement
    • PowerShell Function Parameters
    • PowerShell vs PowerShell ISE
    • PowerShell test-connection
    • PowerShell Test-NetConnection
    • PowerShell GUI
    • PowerShell Variable in String
    • PowerShell Active Directory
  • Operators
    • PowerShell Operators
    • Comparison Operators in PowerShell
    • Logical Operators in PowerShell
    • PowerShell Boolean
    • PowerShell Like Operator
  • cmdlet
    • PowerShell Wait
    • PowerShell Match
    • cmdlets in PowerShell
    • Start PowerShell from cmd
    • Add-Content in PowerShell
    • Get Help in PowerShell
    • PowerShell Copy-Item
    • PowerShell Remove-Item
    • PowerShell Move-Item
    • Get Command in PowerShell
    • PowerShell Run Command
    • Windows PowerShell ISE
    • Windows Powershell Commands
    • WinRM PowerShell
    • PowerShell Date
    • Powershell Write-Host
    • PowerShell Get-ChildItem
    • PowerShell Sort-Object
    • PowerShell Where Object
    • PowerShell Set-Content
    • PowerShell Set-Location
    • PowerShell Invoke-Command
    • PowerShell Invoke-Webrequest
    • PowerShell Get-Location
    • PowerShell Get-Date
    • PowerShell Get-Service
    • PowerShell Test-Path
    • Powershell Module Path
    • PowerShell Out-File
    • PowerShell if File Exists
    • Powershell Copy File
    • PowerShell Delete File
    • PowerShell New-Item
    • PowerShell Rename-Item
    • PowerShell ComputerName
    • PowerShell Get-Content
    • PowerShell Get-Item
    • PowerShell Get-ADUser
    • PowerShell Grep
    • PowerShell Concatenate String
    • PowerShell Get-Process
    • PowerShell Count
    • PowerShell pause
  • Control Statements
    • If Statement in PowerShell
    • If Else in PowerShell
    • Else If in PowerShell
    • Loops in PowerShell
    • For loop in PowerShell
    • PowerShell While Loop
    • PowerShell do while
    • PowerShell Loop through Array
    • PowerShell add to array
    • PowerShell ForEach Loop
    • PowerShell Break
    • PowerShell Continue
    • Switch Case in PowerShell
    • PowerShell If-Not
    • Try-catch in PowerShell
  • Functions
    • PowerShell Functions
    • PowerShell String Functions
    • powershell nslookup
    • PowerShell here string
    • PowerShell Wildcards
    • Regex in PowerShell
    • PowerShell not like
    • PowerShell Filter
    • PowerShell Sleep
    • PowerShell where
    • PowerShell join string
    • PowerShell Exit
    • PowerShell null
    • PowerShell Dictionary
    • PowerShell Location
    • PowerShell Start-Service
    • PowerShell is not digitally signed
    • PowerShell Uptime
    • PowerShell Create Directory
    • PowerShell Trim
    • PowerShell Join-Path
    • PowerShell Execution Policy
    • PowerShell SubString
    • PowerShell Format Table
    • PowerShell Import Module
    • PowerShell ForEach Object
    • PowerShell Alias
    • PowerShell Scheduled Task
    • PowerShell Convert String to Date
    • PowerShell Split String
    • PowerShell Multiline String
    • PowerShell MultiLine Comment
    • PowerShell Rename Folder
    • PowerShell Delete Folder
    • PowerShell String Replace
    • PowerShell join
    • PowerShell xcopy
    • PowerShell Base64
    • PowerShell Tail
    • PowerShell User List
    • PowerShell remove User from group
    • PowerShell JSON Format
    • PowerShell Send Mail
    • PowerShell Convert to String
    • PowerShell Start-Process
    • PowerShell change directory
    • PowerShell Open File
    • PowerShell Batch File
    • PowerShell ZIP
    • PowerShell unzip
    • PowerShell XML
    • PowerShell XML Parsing
    • Remote PowerShell
    • PowerShell Escape Character
    • PowerShell scriptblock
    • PowerShell Executable Location
    • PowerShell Import-CSV?
    • PowerShell Export CSV
  • Interview Questions
    • PowerShell Interview Questions

Related Courses

Shell Scripting Course

All in One Data Science Courses

Data Visualization Courses

PowerShell Environment Variables

By Priya PedamkarPriya Pedamkar

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.

Set-Location Env:
Get-Item Env:

How to Declare PowerShell Environment Variables?

Below are the examples to declare PowerShell Environment Variables:

All in One Data Science Bundle(360+ Courses, 50+ projects)
Python TutorialMachine LearningAWSArtificial Intelligence
TableauR ProgrammingPowerBIDeep Learning
Price
View Courses
360+ Online Courses | 50+ projects | 1500+ Hours | Verifiable Certificates | Lifetime Access
4.7 (86,650 ratings)

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.

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

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
Popular Course in this category
Programming Languages Training (41 Courses, 13+ Projects, 4 Quizzes)
  41 Online Courses |  13 Hands-on Projects |  322+ Hours |  Verifiable Certificate of Completion
4.5
Price

View Course

Related Courses

Shell Scripting Training (4 Courses, 1 Project)4.9
All in One Data Science Bundle (360+ Courses, 50+ projects)4.8
Data Visualization Training (15 Courses, 5+ Projects)4.7
1 Shares
Share
Tweet
Share
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Database Management
  • Machine Learning
  • All Tutorials
Certification Courses
  • All Courses
  • Data Science Course - All in One Bundle
  • Machine Learning Course
  • Hadoop Certification Training
  • Cloud Computing Training Course
  • R Programming Course
  • AWS Training Course
  • SAS Training Course

ISO 10004:2018 & ISO 9001:2015 Certified

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

EDUCBA
Free Data Science Course

SPSS, Data visualization with Python, Matplotlib Library, Seaborn Package

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

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

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

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

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

By signing up, you agree to our Terms of Use and Privacy Policy.

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more