0

I am having this simple two line script to set and remove my HTTP_PROXY when needed .

IF not defined HTTP_PROXY (setx HTTP_PROXY http://user:pass@212.212.212.212:29842) ELSE (REG delete HKCU\Environment /F /V HTTP_PROXY)
IF not defined HTTPS_PROXY (setx HTTPS_PROXY http://user:pass@212.212.212.212:29842) ELSE (REG delete HKCU\Environment /F /V HTTPS_PROXY)

what am trying to do is to trigger the HTTP_PROXY environment .

But when i run as administrator the first time , the value is added and proxy is configured .

Second time , The value is removed and the proxy still configured .

Even when i cant even see the HTTP_PROXY with environment variable i can still get the config with new terminal with echo %HTTP_PROXY%

Am i deleting the value wrong , or my problem is with adding ?

George
  • 2,292
  • 2
  • 10
  • 21
  • 3
    When you use `SetX` the defined variable is not available in the current environment, only in subsequent ones. – Compo Oct 24 '21 at 10:01
  • 3
    to set the environment for both permanent and current session you can do `(setx HTTP_PROXY http://user:pass@212.212.212.212:29842 & set=HTTP_PROXY http://user:pass@212.212.212.212:29842)..` – Gerhard Oct 24 '21 at 10:42
  • Thanks , i know that the setx work like you said , but am trying to remove this and nether ```setx HTTP_PROXY "" ``` or ```setx HTTP_PROXY``` worked for me , my problem is that i need to remove the setx , and ``` REG delete HKCU\Environment /F /V HTTP_PROXY ``` deleting the value from system environment but keep it in cmd somehow . – George Oct 24 '21 at 10:49
  • So you are saying that the HTTP_PROXY is still active after the delete? – Gerhard Oct 24 '21 at 11:02
  • exactly which make no sense at all , even when trying with new command line and do ```curl ifconfig.me``` i still get the old HTTP_PROXY configured , when it has no visible value at Environment Variables on windows . – George Oct 24 '21 at 11:03
  • 2
    Why don't you delete the variables the same way you create them, namely using [`setx`](https://ss64.com/nt/setx.html)? (use `""` to define an empty variable) – aschipfl Oct 24 '21 at 11:06
  • 2
    you simply need to then do `setx HTTP_PROXY ""` to set a nul value, then delete the reg entry, also as mentioned by aschipfl now. – Gerhard Oct 24 '21 at 11:14
  • @aschipfl , actually it worked , but needed few mins , when i tried this i had the impression that it should either work immediately or dont work considering the variable is equal to "" then the proxy will be used and set to "" , Thanks for the answer and please consider answering my question so i mark as accepted . – George Oct 24 '21 at 11:15
  • i ve tried ealier but for some reason its not immediate effect so i assumed it not working , thanks for the help @Gerhard . – George Oct 24 '21 at 11:15
  • 3
    no, it will not be immediate as you need to re-initiate the environment (closing and opening `cmd`). So again back to my first comment, to make it immediate do `setx HTTP_PROXY "" & set HTTP_PROXY=` – Gerhard Oct 24 '21 at 11:16
  • i have no idea if this is a bug with windows 11 or something , but i meant as immediate like opening new command line and trying again , it will need like couple of minutes regrading the new command line , so i will open 5 new CMD and still get the value while there is nothing in the environment variable . its really weird . – George Oct 24 '21 at 11:20
  • 1
    That does not seem possible, but then I do not have Windows 11 so I cannot confirm that. – Gerhard Oct 24 '21 at 11:21
  • yeah its actually really weird , that why i even posted this , the script should be working fine but its acting with this behavior . Thanks a lot for the help . – George Oct 24 '21 at 11:22
  • 1
    Can you confirm that there is not such a value in `HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment` ? – Gerhard Oct 24 '21 at 11:27
  • I have double check now , there is no system variable , nor with reg edit @```HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment```, but still on new CMD , ```echo %HTTP_PROXY``` will return the proxy . – George Oct 24 '21 at 11:31
  • 1
    ok. So I actually went through the upgrade from Windows 10 to Windows 11, just for this. I am sorry to say that I am not experiencing this issue at all. if I `setx HTTP_PROXY ""` it will be empty when I close `cmd` and open it again. I think you should perhaps edit your quesiton and give some more detail including updated code and perhaps some screenshots of proof how it is still active after `setx` – Gerhard Oct 24 '21 at 13:48
  • Oh thank a lot , I've found the answer [here](https://stackoverflow.com/questions/13222724/command-line-to-remove-an-environment-variable-from-the-os-level-configuration/48816308#48816308) apparently the explorer.exe is causing the issue . Thanks a lot for the effort – George Oct 24 '21 at 14:35

1 Answers1

0

The answer was found here .

its related to Applying Values Without Rebooting , the value will be updated but not instantly , it will take some time so the explorer.exe refresh the values .

George
  • 2,292
  • 2
  • 10
  • 21