Windows: Set Environment Variable – CMD & PowerShell

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 note i am showing how to set an environment variable in Windows from the command-line prompt (CMD) and from the Windows PowerShell.

In the examples below i will set an environment variable temporary (for the current terminal session only), permanently for the current user and globally for the whole system.

Cool Tip: Add a directory to Windows %PATH% environment variable! Read More →

Set Environment Variable For The Current Session

Set an environment variable for the current terminal session:

# Windows CMD

# Windows PowerShell

Print an environment variable to the console:

# Windows CMD
C:\> echo %VAR_NAME%

# Windows PowerShell
PS C:\> $env:VAR_NAME

Cool Tip: List Windows environment variables and their values! Read More →

Set Environment Variable Permanently

Run as Administrator: The setx command is only available starting from Windows 7 and requires elevated command prompt. It works both for the Windows command-line prompt (CMD) and the Windows PowerShell.

Permanently set an environment variable for the current user:

C:\> setx VAR_NAME "VALUE"

Permanently set global environment variable (for all users):

C:\> setx /M VAR_NAME "VALUE"

Info: To see the changes after running setx – open a new command prompt.

Was it useful? Share this post with the world!

3 Replies to “Windows: Set Environment Variable – CMD & PowerShell”

  1. Thanks for your help !

  2. krishna avril says: Reply

    AWESOME! Thanks 🙂

  3. Thanks so much!

Leave a Reply