Set the PATH in a Session on Windows

Prepend or append a directory to PATH for the current cmd.exe session only:

set PATH=%PATH%;C:\Some\Other\Directory

Changes made with set are process-local — they vanish when the shell exits and never touch the persistent user/system environment stored in the registry.

Variants

# PowerShell — same session-scoped effect
$env:PATH += ";C:\Some\Other\Directory"
 
# Persistent (user scope, survives reboot — writes to the registry)
setx PATH "%PATH%;C:\Some\Other\Directory"

setx writes to the registry (HKCU\Environment for user scope, HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment with /M for system scope) but does not affect the current session — new shells pick it up. It also truncates values over 1024 characters, which makes it dangerous to use on long existing PATHs; editing via [Environment]::SetEnvironmentVariable in PowerShell is the safer programmatic route.

Security relevance

PATH manipulation cuts both ways:

  • Utility: on a foothold, adding a tools directory to PATH makes dropped binaries callable by bare name without touching their full path in every command.
  • Attack surface: Windows resolves unqualified executable names by searching PATH in order, so a writable directory early in PATH is a classic DLL/binary planting vector — the executable-side cousin of windows-dll-search-order hijacking. User-writable directories in the system PATH, or in a service account’s PATH, are privesc findings.

Sources

Related: windows-dll-search-order, windows-services, powershell-run-commands, unix-set-path-session