How to use environment variables

Overview

Environment variables are named values available to programs. CMD expands a name between percent signs, such as %USERPROFILE%.

set changes variables only in the current CMD process and programs launched from it. Closing the window discards those temporary changes.

  • Use set PATH to display variables whose names begin with PATH.
  • set NAME= removes a temporary variable.
  • Built-in path variables make scripts portable between user accounts.

Commands to try

Type each command at the prompt, then press Enter. Replace sample names and paths with ones that exist on your computer.

Commands are generally not case-sensitive, but preserving the spelling shown here makes scripts easier to read.

echo %USERNAME%
echo %USERPROFILE%
echo %TEMP%
set
set PROJECT=CmdPractice
echo %PROJECT%
set PROJECT=

Useful options and details

Options change how a command behaves. Add spaces exactly as shown, and use the command's built-in help when an option is unfamiliar.

A path containing spaces must be enclosed in double quotes, such as "C:\My Files".

  • set /a evaluates integer arithmetic.
  • set /p NAME=Prompt: reads user input in a batch script.
  • setx writes future environment settings but does not update the current window and can be risky for PATH.

Tips and common mistakes

Check the current folder and read the command output before assuming an operation succeeded.

Practice with disposable files first. Commands that delete, overwrite, or stop a process may not offer an Undo button.

  • Do not put spaces around = unless you intentionally want them in the variable name or value.
  • Undefined variables remain visibly unexpanded, which can create bad paths.
  • Never print secrets in shared screenshots or save them in batch files.

Safe practice

Create a temporary practice folder under your user profile so the examples cannot affect Windows system files.

After each command, inspect the result with dir or another read-only command before continuing.

  • Echo three standard variables.
  • Create and use a temporary PRACTICE variable.
  • Remove it and confirm the result.