Use the SETX command (note the 'x' suffix) to set variables that persist after the cmd window has been closed.

For example, to set an env var "foo" with value of "bar":

setx foo bar /m

Though it's worth reading the 'notes' that are displayed if you print the usage (setx /?), in particular:

  1. On a local system, variables created or modified by this tool will be available in future command windows but not in the current CMD.exe command window.

  2. On a remote system, variables created or modified by this tool will be available at the next logon session.

In PowerShell, the [Environment]::SetEnvironmentVariable command.

Answer from Vik David on Stack Overflow
🌐
Microsoft Learn
learn.microsoft.com › en-us › windows-server › administration › windows-commands › setx
setx | Microsoft Learn
February 3, 2023 - This command provides the only command-line or programmatic way to directly and permanently set system environment values. System environment variables are manually configurable through Control Panel or through a registry editor. The set command, which is internal to the command interpreter (Cmd.exe), sets user environment variables for the current console window only.
🌐
How-To Geek
howtogeek.com › home › windows › how to use windows cmd environment variables
How to Use Windows CMD Environment Variables
April 8, 2022 - The second way uses setx. Setx defines Windows environment variables permanently. They persist between windows and between restarts, and are written to the Windows Registry.
🌐
NTU Singapore
www3.ntu.edu.sg › home › ehchua › programming › howto › Environment_Variables.html
Environment Variables for Java Applications - PATH, CLASSPATH, JAVA_HOME
Windows have two shells: the newer cmd.exe and the older command.com. Each shell come with its own set of commands, utilities, and its own scripting programming language. Unix's variable name is denoted as $varname, e.g., $CLASSPATH. Windows uses %varname%, e,g., %CLASSPATH%. Unix uses command "printenv" (print environment) or "env" to list all the environment variables. Windows uses command "set". Unix's PATH is set permanently in the login or shell initialization script (e.g., "~/.login", "~/.profile", "~/.bashrc", "~/.bash_profile", or "/etc/profile").
🌐
GitHub
gist.github.com › mitchmindtree › 92c8e37fa80c8dddee5b94fc88d1288b
How to permanently set user environment variables on Windows · GitHub
Here are three ways you can set the user environment variable FOO with the value path\to\foo so that the variable persists across sessions.
🌐
SS64
ss64.com › nt › set.html
Set - Environment Variable command - Windows CMD
Changes made using the SET command are NOT permanent, they apply to the current CMD prompt only and remain only until the CMD window is closed. To permanently change a variable at the command line use SetX or with the GUI: Control Panel ➞ System ➞ Environment ➞ System/User Variables Changing ...
🌐
Sigasi
sigasi.com › knowledge › how_tos › setting-environment-variables
Setting Environment Variables - Sigasi
September 23, 2024 - Note: User variables apply only to the current user, while system variables apply to all users on the machine. Open Command Prompt or PowerShell with administrator rights. Run the following command to set a persistent environment variable:
🌐
YouTube
youtube.com › watch
Set Persistent User Environment Variables in Windows (PowerShell & CMD) — No Admin - YouTube
Learn how to set persistent user environment variables in Windows using PowerShell and CMD — without admin rights! This step‑by‑step tutorial shows you how t...
Published   November 23, 2025
Find elsewhere
🌐
Lazy Programmer
lazyprogrammer.me › home › how to set environment variables permanently in windows, linux, and mac
How to Set Environment Variables Permanently in Windows, Linux, and Mac - Lazy Programmer
May 3, 2024 - Go into the search bar, and type in “cmd” to search for the command line tool. Let’s assume you want to set the variable OPENAI_API_KEY to the value "abc". Once the command line program open, run the following command: ... And that’s it! This automatically sets the environment variable permanently...
🌐
ShellHacks
shellhacks.com › home › windows: set environment variable – cmd & powershell
Windows: Set Environment Variable - CMD & PowerShell - ShellHacks
October 28, 2019 - Set an environment variable for the current terminal session: # Windows CMD C:\> set VAR_NAME="VALUE" # Windows PowerShell PS C:\> $env:VAR_NAME="VALUE" ... Run as Administrator: The setx command is only available starting from Windows 7 and requires elevated command prompt.
🌐
NinjaOne
ninjaone.com › home › blog › it ops › how to view and manage environment variables in windows
How to View and Manage Environment Variables In Windows | NinjaOne
March 12, 2026 - To define a temporary variable (valid only for the session): Type set MY_VAR=TestValue and press Enter. This is ideal for temporary overrides or script-based testing. However, it won’t persist once you close the Command Prompt. This method offers more flexibility and control over environment variables. It is useful for both temporary and permanent changes.
🌐
Medium
medium.com › @kbala7092 › how-to-set-environment-variables-using-batch-files-82c0660c09ef
How to Set Environment Variables Using Batch Files | by Bala the Analyst | Medium
April 6, 2025 - A temporary environment variable is available only for the duration of the command prompt session. Once the terminal is closed, the variable is lost. @echo off set MY_VARIABLE=HelloWorld echo %MY_VARIABLE% pause
🌐
Launch School
launchschool.com › books › command_line › read › environment
How to Change your Command Line Environment
Set the variable before a command, on the same line: $ SOMETHING="a value" env ... SOMETHING=a value ... The lifetime of an environment variable set this way is only for the duration of the command.
🌐
Tumblr
forbeslindesay.co.uk › post › 42833119552 › permanently-set-environment-variables-on-windows
Forbes Lindesay — Permanently Set Environment Variables on Windows
February 11, 2013 - This process sucks there should be a better way of doing it, but here are the steps to permanently add an environment variable on Windows. Right click on your computer in the file browser and select properties: Click “Advanced system settings” in the left hand side bar