List, Get, Set, and Unset Windows Environment Variables in Command Prompt

Published: Apr 7, 2021
Updated: Jun 7, 2021

List All Environment Variables #

set

Get an Environment Variable #

Syntax:

echo %<VAR_NAME>%

Usage:

Let’s pretend that your Windows account is named foo. On a traditional Windows machine, here’s what you’d see:

echo %USERNAME% will output foo

echo %USERPROFILE% will output C:\Users\foo

Set an Environment Variable (Only for Current Session) #

Syntax:

set <VAR_NAME>="<VAR_VALUE>"

Usage:

set MY_NAME="Jane Doe"

Set an Environment Variable (Persist) #

Syntax:

setx <VAR_NAME> "<VAR_VALUE>"

Usage:

setx MY_NAME "Jane Doe"

Note: Changes made by setx will only be picked up in new instances of Command Prompt. So, restart Command Prompt to pick up the change.

Unset an Environment Variable (Only for Current Session) #

Syntax:

set <VAR_NAME>=

Usage:

set MY_NAME=

Unset an Environment Variable (Persist) #

Syntax:

reg delete "HKCU\Environment" /v <VAR_NAME> /f

Usage:

reg delete "HKCU\Environment" /v MY_NAME /f

Note: Changes made by reg will only be picked up in new instances of Command Prompt. So, restart Command Prompt to pick up the change.

Docs #