如何在不使用“我的电脑”对话框的情况下在W

 环境配置     |      2021-10-17 17:26

Here's some VBScript I use for this:

set args = WScript.Arguments Set objShell = WScript.CreateObject("WScript.Shell") Set colSystemEnvVars = objShell.Environment("System") Set colUserEnvVars = objShell.Environment("User") ' Parse args select case args.Count case 0, 1, 2 help case 3 sVariable = args(0) sValue = args(1) sScope = UCase(args(2)) sMode = "" case 4 sVariable = args(0) sValue = args(1) sScope = UCase(args(2)) sMode = UCase(args(3)) end select select case sScope case "S" if sMode = "A" then sValue = colSystemEnvVars(sVariable) & sValue end if colSystemEnvVars(sVariable) = sValue case "U" if sMode = "A" then sValue = colUserEnvVars(sVariable) & sValue end if colUserEnvVars(sVariable) = sValue case else help end select WScript.Quit '****************************************************************************** Sub help() WScript.Echo "" WScript.Echo "Create or update an environment variable." WScript.Echo "" WScript.Echo "usage:" WScript.Echo "======" WScript.Echo "cscript SetVar.vbs variable value {S|U} [A]" WScript.Echo "" WScript.Echo "eg:" WScript.Echo "===" WScript.Echo "cscript SetVar.vbs MYVAR 'Hello world' U" WScript.Echo "cscript SetVar.vbs PATH 'C:\MyPath' S A" WScript.Quit End Sub

The scope can be 'S'ystem or 'U'ser. The last argument, 'A', if present, appends the value to the existing value of the variable (useful for adding a directoy to the PATH system variable).

The variables will presist, but you'll have to close then re-open a console to use them. I usually run this from the "Run..." dialog, then open a console.