Update:
The solutions below work in Windows PowerShell and PowerShell (Core) 7 up to v7.4.x
PowerShell v7.5+ is based on .NET 9+, which introduces a breaking change where only passing a
nullas the value to the[Environment]::SetEnvironmentVariable()overload that will remove an environment variable from the current process:In PowerShell, you'll need to use
[NullString]::Value[1] rather than""or$nullwhen calling this .NET API directly in order to remove an environment variable; e.g.:Copy# In v7.5+, only [NullString]::Value actually *removes* the variable. [Environment]::SetEnvironmentVariable('HTTP_PROXY', [NullString]::Value)- Note: If you use
''(the empty string), you'll now get the following behavior:- With the (default)
Processscope, i.e. for in-process environment variables, the variable will be created with (or set to) to the empty string. - With the persistent
UserorMachinescopes (supported on Windows only), a registry value that defines the variable with an empty string value technically is created, but such a value has no effect and is tantamount to removal of the variable; the reason is that when Windows instantiates a process' environment based on the persisted definitions in the registry, it ignores those whose value is the empty string.
- With the (default)
- Note: If you use
As per the decision published here, v7.5+ surfaced this breaking change PowerShell-natively too; that is:
Only assigning
$null, i.e.$env:HTTP_PROXY = $nullin the case at hand, will result in an environment variable's removal going forward.$env:HTTP_PROXY = ''will define this variable as / set it to the empty string rather than removing it.
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:
On Unix-like platforms you'd have to use alias
riinstead ofrm, or use the cmdlet's full name,Remove-Item, instead; e.g., without usingSet-Locationfirst,ri Env:HTTP_PROXY.See also: about_Environment_Variables.
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).
Note that setting / removing machine-level variables (
'Machine') requires elevation (running as admin).[See update at the top] By design, any of the following values causes the specified environment variable to be deleted:
'',[NullString]::Value(the equivalent ofnullin C#[1]),"`0"(a singleNUL(0x0) char.)- As an aside: This permissiveness is problematic, as it prevents creating environment variables whose value is the empty string, which is not uncommon on Unix-like platforms - see GitHub issue #50554.
Note that these methods remove only the persistent definitions of these variables - any definitions in the current session (process) 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
Rundialog (WinKey-R) and submitpowershell -noprofile, then check if these variables are still present.
- To rule out this possibility, use the Windows
[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.
Update:
The solutions below work in Windows PowerShell and PowerShell (Core) 7 up to v7.4.x
PowerShell v7.5+ is based on .NET 9+, which introduces a breaking change where only passing a
nullas the value to the[Environment]::SetEnvironmentVariable()overload that will remove an environment variable from the current process:In PowerShell, you'll need to use
[NullString]::Value[1] rather than""or$nullwhen calling this .NET API directly in order to remove an environment variable; e.g.:Copy# In v7.5+, only [NullString]::Value actually *removes* the variable. [Environment]::SetEnvironmentVariable('HTTP_PROXY', [NullString]::Value)- Note: If you use
''(the empty string), you'll now get the following behavior:- With the (default)
Processscope, i.e. for in-process environment variables, the variable will be created with (or set to) to the empty string. - With the persistent
UserorMachinescopes (supported on Windows only), a registry value that defines the variable with an empty string value technically is created, but such a value has no effect and is tantamount to removal of the variable; the reason is that when Windows instantiates a process' environment based on the persisted definitions in the registry, it ignores those whose value is the empty string.
- With the (default)
- Note: If you use
As per the decision published here, v7.5+ surfaced this breaking change PowerShell-natively too; that is:
Only assigning
$null, i.e.$env:HTTP_PROXY = $nullin the case at hand, will result in an environment variable's removal going forward.$env:HTTP_PROXY = ''will define this variable as / set it to the empty string rather than removing it.
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:
On Unix-like platforms you'd have to use alias
riinstead ofrm, or use the cmdlet's full name,Remove-Item, instead; e.g., without usingSet-Locationfirst,ri Env:HTTP_PROXY.See also: about_Environment_Variables.
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).
Note that setting / removing machine-level variables (
'Machine') requires elevation (running as admin).[See update at the top] By design, any of the following values causes the specified environment variable to be deleted:
'',[NullString]::Value(the equivalent ofnullin C#[1]),"`0"(a singleNUL(0x0) char.)- As an aside: This permissiveness is problematic, as it prevents creating environment variables whose value is the empty string, which is not uncommon on Unix-like platforms - see GitHub issue #50554.
Note that these methods remove only the persistent definitions of these variables - any definitions in the current session (process) 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
Rundialog (WinKey-R) and submitpowershell -noprofile, then check if these variables are still present.
- To rule out this possibility, use the Windows
[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.
A shorter alternative that can be used also in scripts:
Copyrm Env:HTTP_PROXY
rm Env:HTTPS_PROXY
That way you don't change location when removing the environment variables
Need to remove an entry from $env:path
Delete a value in system variable path
[Environment]::SetEnvironmentVariable(..., $null, 'User') does not fully remove user environment variables since PowerShell 7.5
Removing items from $env:PATH
How to set an environment variable using PowerShell?
To set an environment variable, you can use the following syntax.
$Env:VariableName = “Value”
Replace VariableName with the desired name of your variable and Value with the value you want to assign to it, as example below.
$Env:MY_VAR = “MyValue”
How to check if an environment variable exists in PowerShell?
To check if a specific environment variable exists, you can use the following command.
Test-Path Env:MY_VAR Replace MY_VAR with the name of the environment variable you want to check. If the variable exists, it will return True; if not, it will return False.
How do I list all variables in PowerShell?
To list all variables in your PowerShell session, you can use:
Get-ChildItem Env:
This command will show all currently defined variables in the session, including environment variables.
Videos
If you enter the following in PowerShell
$env:path -split ';'
There is an entry that I would like to remove completely whilst leaving everything else intact.
Let's say for arguments sake the entry I wanted removing is: C:\Windows\System32\OpenSSH
How would I achieve this?
The only command I know is Remove-ItemProperty, which will remove an entire value.
I found the following which didn't work
$path = $env:Path
$newpath = $path.replace("C:\Windows;","")
$env:Path = $newpathAny ideas how this can be achieved?
Hello,
I am wondering if you could point me in the right direction, I am trying to figure out how to remove/delete a single value of the Path variable in System Environment. As you know, the Path variable usually has multiple values, I don’t want to remove all the values, just one particular one. So far, the documents I’ve read online keeps showing to either remove the variable itself completely or remove all the values in the variable from the env: or from registry by using Clear-Item or Clear-ItemProperty. I am lost. Thank you for your help and please let me know if you have any questions.
Thank you,
Nhan
Answers are given for both temporarily change Path ($env:Path) or permanently (windows registry)
Be extremely careful if you are looking to edit the registry. Ideally you would save it to a file in case something got changed that you weren’t intending to change
https://stackoverflow.com/questions/714877/setting-windows-powershell-environment-variables
Hi guys,
due to a rogue script running wild I have a domain-wide problem: Every GPO-iteration the script added a string to PATH. Now I want to get rid of it, but I am really not sure on how to do this. Has anyone a sleek example at hand? I already tried using a quick hack:
$ENV:PATH > C:\tmp
gc tmp | Foreach-Object {$_ -replace "stringtogetridof", ""} | Set-Content tmp2but it did not work - nothing replaced.
Thx for your insights!
One thing I find useful, from the command line, is to se Get-Chiditem to ensure you have the files then pipe the command to Remove-Item. So
Get-ChildItem -path
If that shows the right files - then just up arrow, click end and add:
Get-ChildItem -path | Remove-Item -force
This way, you work out that you had the path wrong as pointed out by @francishagyard2 .
Background: I want a script to run for new users and only once so I made a very short cmd the starts a powershell script that creates a shortcut that points to a cmd that deletes 3 folders if there is no flag and then creates a flag and deletes the shortcut that started it.
The powershell script creates a shortcut in:
$Shortcut = $WshShell.CreateShortcut("C:\Users\Default\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup\delete firefox profile.lnk")
Then when a new user logs in that becomes a shortcut in:
"$Env:AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup\delete firefox profile.lnk"
I logged in as a test user and I’m looking at the file as I type but when I try to delete it in cmd or powershell I’m told that the file does not exist.
I can delete it if I reference the explicit location of the file:
Remove-Item "C:\Users\testdept\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup\delete firefox profile.lnk"
but if I use and environment variable I get the following:
Remove-Item "$Env:AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup\delete firefox profile.lnk"
Remove-Item : Cannot find path 'C:\Users\testdept\AppData\Roaming\Roaming\Microsoft\Windows\Start
Menu\Programs\Startup\delete firefox profile.lnk' because it does not exist.
At line:1 char:1
+ Remove-Item "$Env:AppData\Roaming\Microsoft\Windows\Start Menu\Progra ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (C:\Users\testde...fox profile.lnk:String) [Remove-Item], ItemNotFoundEx
ception
+ FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.RemoveItemCommand
I get the same thing in cmd when using %userprofile% which translates to the explicit path
del "%userprofile%\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Start-up\delete firefox profile.lnk" /q/s
gets me
del "C:\Users\testdept\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Start-up\deletefirefoxprofile.lnk" /q/s
The system cannot find the file specified.
and since I want to do this for new users I need to use variables