Existe-t-il une commande pour rafraîchir les variables d'environnement depuis l'invite de commandes sous Windows ?

ForumBot
Messages : 26117
Inscription : mer. avr. 22, 2026 5:33 pm

Existe-t-il une commande pour rafraîchir les variables d'environnement depuis l'invite de commandes sous Windows ?

Message par ForumBot »

Existe-t-il une commande pour rafraîchir les variables d’environnement depuis l’invite de commandes sous Windows ?

ayi
Site Admin
Messages : 13176
Inscription : mer. avr. 22, 2026 5:21 pm

Re: Existe-t-il une commande pour rafraîchir les variables d'environnement depuis l'invite de commandes sous Windows ?

Message par ayi »

Vous pouvez capturer les variables d’environnement système avec un script VBS, but you need a bat script to actually change the current environment variables, so this is a combined solution.


Create a file named resetvars.vbs containing this code, and save it on the path:


`Set oShell = WScript.CreateObject(“WScript.Shell”)

filename = oShell.ExpandEnvironmentStrings(“%TEMP%\resetvars.bat”)

Set objFileSystem = CreateObject(“Scripting.fileSystemObject”)

Set oFile = objFileSystem.CreateTextFile(filename, TRUE)


set oEnv=oShell.Environment(“System”)

for each sitem in oEnv

oFile.WriteLine("SET " & sitem)

next

path = oEnv(“PATH”)


set oEnv=oShell.Environment(“User”)

for each sitem in oEnv

oFile.WriteLine("SET " & sitem)

next


path = path & “;” & oEnv(“PATH”)

oFile.WriteLine(“SET PATH=” & path)

oFile.Close



create another file name resetvars.bat containing this code, same location:

`@echo off
%~dp0resetvars.vbs
call "%TEMP%\resetvars.bat"


When you want to refresh the environment variables, just run resetvars.bat


Apologetics:


The two main problems I had coming up with this solution were


a. I couldn’t find a straightforward way to export environment variables from a vbs script back to the command prompt, and


b. the PATH environment variable is a concatenation of the user and the system PATH variables.


I’m not sure what the general rule is for conflicting variables between user and system, so I elected to make user override system, except in the PATH variable which is handled specifically.


I use the weird vbs+bat+temporary bat mechanism to work around the problem of exporting variables from vbs.


Note: this script does not delete variables.


This can probably be improved.


ADDED


If you need to export the environment from one cmd window to another, use this script (let’s call it exportvars.vbs):


`Set oShell = WScript.CreateObject(“WScript.Shell”)

filename = oShell.ExpandEnvironmentStrings(“%TEMP%\resetvars.bat”)

Set objFileSystem = C


(Réponse tronquée)

Répondre

Revenir à « Performance & Dépannage »