powershell

Cannot remove PowerShell environment variables


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.


Solution

  • Update:


    In-process removal:

    $env:HTTP_PROXY=''
    $env:HTTPS_PROXY=''
    [Alternatively (though it's simpler to use e.g., rm env:HTTP_PROXY):]
    Set-Location Env:
    rm HTTP_PROXY
    rm HTTPS_PROXY

    The statements above remove the specified environment variables from the current session (process) only.

    Note:

    However, if these environment variables are defined persistently, via the registry (on Windows), they will resurface in future sessions.


    Persistent removal (Windows only):

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

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

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


    [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 parameter.