win 快速添加环境变量 Path 脚本

更新了一下


PathRight

:bullseye: 一个智能的 Windows 工具,让您通过右键菜单轻松管理系统 PATH 环境变量。

:sparkles: 特色功能

  • :computer_mouse: 通过右键"发送到"菜单,一键添加路径到系统 PATH

  • :brain: 智能路径处理:

  • 选择文件夹时:直接添加该文件夹到 PATH

  • 选择文件时:自动添加该文件的父文件夹到 PATH

  • :light_bulb: 重复路径智能检测,避免重复添加

  • :high_voltage: 无需打开系统设置,即可完成操作

:inbox_tray: 安装方法

  1. 下载 add2path.batadd2path_install.bat 两个文件

  2. 以管理员身份运行 add2path_install.bat 完成安装

:rocket: 使用方法

  1. 在要添加到 PATH 的文件或文件夹上右键

  2. 选择 “发送到” → “add2path”

  3. 完成!路径已成功添加到系统 PATH 中

:warning: 注意事项

  • 需要管理员权限才能修改系统环境变量

  • 添加路径后可能需要重启命令行或应用程序才能生效

:folded_hands: 致谢

安装脚本参考了 CapsWriter-Offline-CLI 的实现

20 Likes

tieba_009
快过年了大家都不逛论坛了是吧

2 Likes

大佬太强了 :tieba_087:

1 Like

挚友来了

1 Like

诶嘿,有点意思。谢谢佬。
Mark一下,我学会了。

佬还是:cow:

太强了,大佬

学习一下

爱了爱了,感谢分享

佬友太强了!

1 Like

感谢分享实用技巧。添加环境变量已经轻车熟路,但是需要点击、按键多次才能完成操作。思路值得借鉴。

1 Like

能把这个加到右键菜单吗

1 Like

好主意,我一会研究一下

2 Likes

哈哈哈,还是很实用,学习了

1 Like

我还在上班呢

1 Like

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\""

测试过了吗?好像不全呢?

修改了一下, 需要选中目录按shift+right key

setx PATH “%PATH%;c:\111\bin;c:\222\bin” /M

更新了,很方便