Skip to content

Instantly share code, notes, and snippets.

@etcetra7n
Last active April 22, 2025 13:33
Show Gist options
  • Save etcetra7n/569ee8a34eb773e5de917f653f921e0a to your computer and use it in GitHub Desktop.
Save etcetra7n/569ee8a34eb773e5de917f653f921e0a to your computer and use it in GitHub Desktop.
Windows environment PATH variable right click automation

Windows environment PATH variable right click automation for developers

demonstration image

By following the below instruction, you can create a right click context menu saying "Add to PATH" whenever you right click on a folder/directory

  1. Save AddPath.ps1 to C:\cmdhost
  2. Click here to download the icon file. Save this file to F:\icons\AddPath.ico
  3. Download AddPath-Menu.reg
  4. Run AddPath-Menu.reg by double clicking it

Note 1: If you are not saving the files in the locations mentioned, update the reg file accordingly to reflect the actual file locations

Note 2: The below configuration only shows the menu on Shift+Right Click. If you want the menu to be visible in plain Right Click, remove all "Extended"="" from the reg file

Note 3: The new path entries are always added to the end of $PATH variable

Features

  • Loss protection: You don't have to worry about losing your PATH variable, since AddPath.ps1 creates backup at C:\cmdhost\.env_path_backup before altering the $PATH variable. So you could manually restore anytime, if the script fails and corrupts your $PATH
  • Duplicate prevention: No duplicate paths will be added to your $PATH since whenever you add a path the script checks whether the path already exists before altering the $PATH
Windows Registry Editor Version 5.00
[HKEY_CLASSES_ROOT\Directory\Background\shell\AddPath]
@="Add to PATH"
"Extended"=""
"Icon"="F:\\icons\\AddPath.ico"
[HKEY_CLASSES_ROOT\Directory\Background\shell\AddPath\command]
@="powershell -File C:\\cmdhost\\AddPath.ps1 -arg1 \"%1\""
[HKEY_CLASSES_ROOT\Directory\shell\AddPath]
@="Add to PATH"
"Extended"=""
"Icon"="F:\\icons\\AddPath.ico"
[HKEY_CLASSES_ROOT\Directory\shell\AddPath\command]
@="powershell -File C:\\cmdhost\\AddPath.ps1 -arg1 \"%1\""
param (
[string]$arg1
)
$oldPath = [Environment]::GetEnvironmentVariable("PATH", "User")
echo "Old PATH: $oldPath`n"
$oldPath | Out-File -FilePath $PSScriptRoot\.env_path_backup -NoNewline
echo "Your old PATH is backed up at $PSScriptRoot\.env_path_backup`n"
if (-not($oldPath.Split(';') -contains $arg1)) {
if ($oldPath.EndsWith(";")){
$newPath = "$oldPath$arg1;"
} else {
$newPath = "$oldPath;$arg1;"
}
[Environment]::SetEnvironmentVariable("PATH", $newPath, "User")
echo "New PATH:"
$updatedPath = [Environment]::GetEnvironmentVariable("PATH", "User")
echo $updatedPath.Replace(";", "`n")
} else {
echo "This path already exists`n"
}
pause
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment