Learning and Sharing
  • Home
  • Blog
  • Linux
  • macOS
  • Virtualization
    • VMware
    • VirtualBox
  • Windows
    • Windows 11
    • Windows 10
    • Windows Server
  • Series
    • Symantec
    • Intune
    • Microsoft Azure
    • Powershell
    • VirtualBox
    • VMware
    • PowerShell Learning
    • Microsoft Graph
  • More
    • Auto Installation
    • AEC Installation
  • Contact
No Result
View All Result
  • Home
  • Blog
  • Linux
  • macOS
  • Virtualization
    • VMware
    • VirtualBox
  • Windows
    • Windows 11
    • Windows 10
    • Windows Server
  • Series
    • Symantec
    • Intune
    • Microsoft Azure
    • Powershell
    • VirtualBox
    • VMware
    • PowerShell Learning
    • Microsoft Graph
  • More
    • Auto Installation
    • AEC Installation
  • Contact
No Result
View All Result
No Result
View All Result

How to Set Environment Variable in Windows Using CMD & PowerShell

July 26, 2024
in A, Blog, Powershell
0
ADVERTISEMENT

Table of Contents

What is an environment variable in Windows?

An environment variable is a dynamic object containing an editable value which may be used by one or more software programs in Windows.

In this post, we will show you how to set an environment variable in Windows from the command-line prompt (CMD) and from the Windows PowerShell. In the examples below we will set an environment variable temporary (for the current terminal session only), permanently for the current user and globally for the whole system.

Environment variable scopes

In Windows, there’re three scopes to set an environment variable:

  • Session scope: The environment variables valid in the current session only. Closing the PowerShell window will revert the environment variables to its pre-determined state
  • User scope: User environment variables are specific only to the currently logged-in user.
  • System scope: System environment variables are globally accessed by all users.
PZYOB7XxNDHkveS0DMtGuHwSdFdwfcLJigCrBJY3stuL5qLlJTar4YDzJBZv

From Windows, you can add an environment variable with GUI from Control Panel. But this post will focus on command line for automation and scripting.

Set Environment Variable in Windows Using PowerShell

In PowerShell we can get the list of environment variables using the Get-ChildItem cmdlet.

$Env:PATH -split ";"
Name                           Value
----                           -----
ALLUSERSPROFILE                C:\ProgramData
APPDATA                        C:\Users\admin\AppData\Roaming
ChocolateyInstall              C:\ProgramData\chocolatey
ChocolateyLastPathUpdate       133396374426589550
CommonProgramFiles             C:\Program Files\Common Files
CommonProgramFiles(x86)        C:\Program Files (x86)\Common Files
CommonProgramW6432             C:\Program Files\Common Files
COMPUTERNAME                   DESKTOP-B2TOHJT
ComSpec                        C:\Windows\system32\cmd.exe
DriverData                     C:\Windows\System32\Drivers\DriverData
HOMEDRIVE                      C:
HOMEPATH                       \Users\admin
LOCALAPPDATA                   C:\Users\admin\AppData\Local
LOGONSERVER                    \\DESKTOP-B2TOHJT
NUMBER_OF_PROCESSORS           12
...

Set Environment Variable for the Current Session using PowerShell

To set an environment variable for the current terminal session. Use the below syntax:

$env:VAR_NAME="VALUE"

For example, we will create an environment variable named BACKUPREPO with is value is the path of the folder D:\Backup.

$env:BACKUPREPO="D:\Backup"

To get the value of an environment variable, use the following syntax $env:VAR_NAME.

dsVfJv35rHFp071IE4W1GTcyNA7jQ2h4288CLQd6g67GbofdqQnnbwfvDnG7

Get the list of environment variables, you should see the newly created environment variable in the list.

PS D:\Backup> Get-ChildItem Env:

Name                           Value
----                           -----
ALLUSERSPROFILE                C:\ProgramData
APPDATA                        C:\Users\mpnadmin\AppData\Roaming
BACKUPREPO                     D:\Backup
ChocolateyInstall              C:\ProgramData\chocolatey
ChocolateyLastPathUpdate       133659380944392780
CLIENTNAME                     DESKTOP-B2TOHJT
CommonProgramFiles             C:\Program Files\Common Files
CommonProgramFiles(x86)        C:\Program Files (x86)\Common Files
CommonProgramW6432             C:\Program Files\Common Files
...

From now, we can use the environment variable instead of the full path of the folder. For example, we can navigate to the backup repository using the environment variable.

ciYDDPN5FzpN30xlE544F9kdfRWwxyox1cM4TGXEV2fLXfrgUbyuVfVBpFgm

Set Environment Variable Permanently using PowerShell

Using the $env:VAR_NAME will only temporarily add the environment variable into the current PowerShell session. Closing the PowerShell window will revert the environment variables to its pre-determined state. To permanently set an environment variable, we can use the following below methos.

You can use the below command to add an environment variable permanently with user or machine scope:

# Permanently set an environment variable for the current user:
# setx /M VAR_NAME "VALUE"
setx BACKUPREPO "D:\Backup"

# Permanently set global environment variable (for all users):
# setx /M VAR_NAME "VALUE"
setx /M BACKUPREPO "D:\Backup"
jHanDCX4UaVpAjKtGnYNm1CnLA0WkaOIDuRdbCWIPNCvO93ZrX2l1PXfwRhH

Close all running PowerShell windows then reopen a new one to verify it works.

Set Environment Variable in Windows Using CMD

In case you want to do it using CMD (Command Prompt):

Set an environment variable for the current terminal session:

set VAR_NAME="VALUE"

When you need to add an environment variable permanently using CMD. The command is the same as doing in PowerShell:

# Permanently set an environment variable for the current user:
# setx /M VAR_NAME "VALUE"
setx BACKUPREPO "D:\Backup"

# Permanently set global environment variable (for all users):
# setx /M VAR_NAME "VALUE"
setx /M BACKUPREPO "D:\Backup"
5/5 - (1 vote)
Previous Post

How to Add a Directory to Windows %PATH% Environment Variables

Next Post

How to Get the PATH Environment Variables Using PowerShell

Related Posts

Images Hidden Due To Mature Content Settings In CivitAI

August 31, 2024

Azure OpenAI vs Azure AI Hub, How to Choose the Right One for Your Needs

August 20, 2024

Running Hyper-V and VMware Workstation on The Same Machine

August 15, 2024

How to Uninstall All Autodesk Products At Once Silently

July 29, 2024
Ftr5

How to Uninstall the Autodesk Genuine Service on Windows

July 29, 2024

How to Remove The Test Mode Watermark Without Disabling Test Mode

July 28, 2024

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Recent Posts

  • How To Turn On uBlock Origin Extension in Chrome (2025)
  • Images Hidden Due To Mature Content Settings In CivitAI
  • Azure OpenAI vs Azure AI Hub, How to Choose the Right One for Your Needs

Categories

Stay in Touch

Discord Server

Join the Discord server with the site members for all questions and discussions.

Telegram Community

Jump in Telegram server. Ask questions and discuss everything with the site members.

Youtube Channel

Watch more videos, learning and sharing with Leo ❤❤❤. Sharing to be better.

Newsletter

Join the movement and receive our weekly Tech related newsletter. It’s Free.

General

Microsoft Windows

Microsoft Office

VMware

VirtualBox

Technology

PowerShell

Microsoft 365

Microsoft Teams

Email Servers

Copyright 2025 © All rights Reserved. Design by Leo with ❤

No Result
View All Result
  • Home
  • Linux
  • Intune
  • macOS
  • VMware
  • VirtualBox
  • Powershell
  • Windows 10
  • Windows 11
  • Microsoft 365
  • Microsoft Azure
  • Microsoft Office
  • Active Directory

No Result
View All Result
  • Home
  • Linux
  • Intune
  • macOS
  • VMware
  • VirtualBox
  • Powershell
  • Windows 10
  • Windows 11
  • Microsoft 365
  • Microsoft Azure
  • Microsoft Office
  • Active Directory