Save this script as AddToSystemPath.ps1 (e.g., in your Documents folder)
# Get the folder path from the right-click context menu (passed as argument %1)
$folderPath = $args[0]
# Get the current system Path environment variable
$currentPath = [Environment]::GetEnvironmentVariable('Path', 'Machine')
# Check if the path is already in the system Path (to avoid duplicates)
if ($currentPath -notlike "*$folderPath;*") {
# Append the new folder path to the system Path (with a semicolon separator)
$newPath = $currentPath + ";" + $folderPath
try {
# Set the new system Path environment variable
[Environment]::SetEnvironmentVariable('Path', $newPath, 'Machine')
# Inform the user that the path has been added
Write-Host "Successfully added '$folderPath' to System Path."
Write-Host "You may need to restart applications for changes to take effect."
# Optional: Show a message box (requires System.Windows.Forms assembly)
#[System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
#[System.Windows.Forms.MessageBox]::Show("Successfully added '$folderPath' to System Path.`nYou may need to restart applications for changes to take effect.", "System Path Updated", [System.Windows.Forms.MessageBoxButtons]::OK, [System.Windows.Forms.MessageBoxIcon]::Information)
} catch {
Write-Error "Error adding to System Path: $_"
# Optional: Show an error message box
#[System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
#[System.Windows.Forms.MessageBox]::Show("Error adding to System Path:`n$_", "Error", [System.Windows.Forms.MessageBoxButtons]::OK, [System.Windows.Forms.MessageBoxIcon]::Error)
}
} else {
Write-Host "'$folderPath' is already in the System Path."
# Optional: Show a message box that the path is already present
#[System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
#[System.Windows.Forms.MessageBox]::Show("'$folderPath' is already in the System Path.", "Already in System Path", [System.Windows.Forms.MessageBoxButtons]::OK, [System.Windows.Forms.MessageBoxIcon]::Information)
}
Here’s the updated AddToSystemPath.reg file (shift), 并修改D:\temp\down\1exe1\mytools\add2path.ps1这个路径:
Windows Registry Editor Version 5.00
[HKEY_CLASSES_ROOT\Folder\shell\AddToSystemPath]
@="Add to System Path"
"Extended"=""
[HKEY_CLASSES_ROOT\Folder\shell\AddToSystemPath\command]
@="powershell.exe -NoProfile -ExecutionPolicy Bypass -File \"D:\\temp\\down\\1exe1\\mytools\\add2path.ps1\" \"%1\""