如何在vbs中非持久地更改环境变量

时间:2018-02-17 13:00:44

标签: windows vbscript

Set Shell=WScript.CreateObject("WScript.Shell")
Set Environment=Shell.Environment("User")
Environment("Path")=Environment("Path")&"C:\"

我想更改运行脚本的wscript.exe进程的环境变量,仅影响它及其子进程。上面的代码不是我想要的,因为它更改了HKEY_CURRENT_USER \ Environment注册表设置,它会影响每个新进程。换句话说,我需要cmd.exe中set命令的对应部分

1 个答案:

答案 0 :(得分:5)

您必须更改process环境:

With CreateObject("WScript.Shell")
    With .Environment("process")
        .Item("path") = .Item("path") & ";C:\"
    End With
    .Run "wscript.exe ""C:\test\another.vbs"""
End With