How to set a variable for the current OS session only

前端 未结 3 559
一整个雨季
一整个雨季 2021-01-14 06:27

setx permanently modifies environment variables.
set only makes variables available during batch script duration.
Is there any way to set a

3条回答
  •  挽巷
    挽巷 (楼主)
    2021-01-14 07:25

    Ok I found a much simpler (and obviously less hacky) way than the JScript method, but it put us in the right direction with the volatile environment manipulation.

    Simply use the reg command to manipulate the volatile environment in the registry :

    reg add "HKCU\Volatile Environment" /v "%MYVAR%" /d "%MYVALUE%" /f
    

    reg add both can create and update the value.

    \f bypasses the overwrite confirmation.

    Setting an empty data with \d "" is equivalent to deleting the global variable. You won't see it in the shell with set, but you can still see it being empty in the registry.

    Here is a reference: https://ss64.com/nt/reg.html

提交回复
热议问题