Home > Back-end >  Cannot remove PowerShell environment variables
Cannot remove PowerShell environment variables

Time:11-16

I set two environment variables HTTP_PROXY and HTTPS_PROXY in Windows PowerShell before. Now I want to remove them but I find that no matter how I type the command, the next time I open PowerShell, these two variables will appear again.

Here are the commands I have tried:

$env:HTTP_PROXY=''
$env:HTTPS_PROXY=''

[Environment]::SetEnvironmentVariable('HTTP_PROXY', '', 'User')
[Environment]::SetEnvironmentVariable('HTTPS_PROXY', '', 'User')
[Environment]::SetEnvironmentVariable('HTTP_PROXY', '', 'Machine')
[Environment]::SetEnvironmentVariable('HTTPS_PROXY', '', 'Machine')

Set-Location Env:
rm HTTP_PROXY
rm HTTPS_PROXY

But none of them works.

CodePudding user response:

$env:HTTP_PROXY=''
$env:HTTPS_PROXY=''
Alternatively:
Set-Location Env:
rm HTTP_PROXY
rm HTTPS_PROXY

These statements remove the specified environment variables from the current session only (note that on Unix-like platforms you'd have to use alias ri instead of rm or the cmdlet's full name, Remove-Item, instead).

If they're defined persistently, via the registry (on Windows), they will resurface in future sessions.


[Environment]::SetEnvironmentVariable('HTTP_PROXY', '', 'User')
[Environment]::SetEnvironmentVariable('HTTPS_PROXY', '', 'User')
[Environment]::SetEnvironmentVariable('HTTP_PROXY', '', 'Machine')
[Environment]::SetEnvironmentVariable('HTTPS_PROXY', '', 'Machine')

These statements - assuming they execute without triggering an exception - do remove the persistent definitions (on Windows only).

  • Note that setting / removing machine-level variables ('Machine') requires elevation (running as admin).

  • By design, any of the following values causes the specified environment variable to be deleted: '', [NullString]::Value (the equivalent of null in C#[1]), "`0" (a single NUL (0x0) char.)

  • Note that these methods remove only the persistent definitions of these variables - any definitions in the current session are left untouched; to also remove them, use the methods at the top.

If the environment variables unexpectedly still resurface in future sessions, there are two potential causes:

  • Perhaps you started a new session directly from the old session, e.g. with Start-Process powershell.exe - in that case the current session's environment variables are inherited by the new session, so unless you've removed the environment variable from the current session as well, the new session will see them.

  • There may be code in your profile files, notably $PROFILE, that (re)defines these environment variables whenever a new session starts.

    • To rule out this possibility, use the Windows Run dialog (WinKey-R) and submit powershell -noprofile, then check if these variables are still present.

[1] PowerShell does have a $null constant that is generally the equivalent of C#'s null, but in a string context PowerShell forces $null values to '' (the empty string). Therefore, the [NullString]::Value singleton is required in order to pass a genuine null value to a string-typed .NET method.

  • Related