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 Variable
Secondary Sidebar
What is MapReduce in Hadoop?

Virtualization in Cloud Computing

Bias-Variance

MongoDB vs Postgres

Oracle Java

Data Analysis Tools

PowerShell Environment Variable

PowerShell Environment Variable

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.

Syntax:

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

The syntax to fetch the value of an environment variable is as follows

$Env:<name of the variable>

Eg:

Input:

$Env: Path

Output:

scope

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

PSExecutionPolicyPreference:

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.

PSDisableModuleAnalysisCacheCleanup:

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.

PSModulePath:

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.

Example:

Input:

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
Get-ChildItem Env:
Write-Host "Sorting the environment variables by name" -ForegroundColor Green
Get-ChildItem Env: | Sort Name

Output:

powershell 4

powershell 3

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.

Example

Input:

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'

Output:

powershell 2

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.

Example:

Input:

Write-Host "Demo of setting variable using .net methods" -ForegroundColor Green
$tp = [Environment]::GetEnvironmentVariable('PSModulePath', 'Machine')
Write-Host $tp
Write-Host "Setting a environment variable" -ForegroundColor Green
[System.Environment]::SetEnvironmentVariable('test', 'test',[System.EnvironmentVariableTarget]::Machine)
$tp1=[Environment]::GetEnvironmentVariable('test', 'Machine')
Write-Host "value is" -ForegroundColor Green
$tp1

Output:

powershell 1

Conclusion

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.

Recommended Articles

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 –

  1. PowerShell XML
  2. PowerShell Wait
  3. PowerShell change directory
  4. PowerShell where
Popular Course in this category
PowerShell Training (2 Courses, 1 Project)
  2 Online Courses |  1 Hands-on Project |   4+ 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
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

© 2023 - 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

Let’s Get Started

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
EDUCBA

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

Forgot Password?

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