You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[Environment]::SetEnvironmentVariable(..., $null, 'User') does not fully remove user environment variables since PowerShell 7.5 #27414
Let me attempt a summary that can be pointed to should similar issues get raised again:
.NET 9 introduced an intentional, breaking change that now requires passing null to the value parameter of [Environment]::SetEnvironmentVariable() in order to achieve removal of an environment variable; up to .NET 8, you could alternatively use the empty string (""), which in .NET 9+ now defines (or updates) a variable with the empty string as the value, for better cross-platform compatibility (and to fully surface the capabilities of the underlying WinAPI, which always supported empty env. vars., albeit only in-process ones, not persisted definitions - see below).
On Windows only, where said method additionally allows managing persistent env. var. definitions, in the Windows registry (via scope values of User and Machine), passing the empty string (instead of null) in effect still results in removal: technically, the definition is then created / retained with the empty string as the value in registry, but Windows ignores such definitions when it populates a process' environment block.
It's best to avoid relying on this, however, both for conceptual clarity and to make code future-proof.
PowerShell decided to surface this breaking change as-is, which landed in v7.5; the implications for PowerShell 7.5+ are:
In the context of PowerShell's namespace variable notation (which acts on in-process env. vars. only), this translates to:
$env:FOO = $nullremoves env. var. FOO
whereas $env:FOO = '' defines (or updates) FOO with the empty string as the value.
In the context of a direct .NET API call, [NullString]::Value must be used to pass a true null value, i.e. to effect removal.
E.g., the equivalent of $env:FOO = $null is [Environment]::SetEnvironmentVariable('FOO', [NullString]::Value)
or, with the implied scope parameter value spelled out, [Environment]::SetEnvironmentVariable('FOO', [NullString]::Value, 'Process')
Note that [NullString]::Value to pass a true null to [string]-typed method parameters is a general requirement, due to PowerShell forcing [string]-typed values to be non-null (e.g., '' -eq [string] $null is $true)
Note:
A simple way to avoid the conceptual obscurity stemming from having to assign a special value in order to remove an environment variable is to use the provider cmdlets on the Env: drive - however, note that this only works on in-process env. vars.
There is a seemingly long-since abandoned RFC that was meant to introduce cmdlets specifically for environment-variable management. My personal take is that it would have made more sense to stick with the provider model and simply add a -Scope parameter to the provider cmdlets as a dynamic parameter of the environment provider, on Windows, to allow targeting the persistent definitions.
E.g., instead of $env:FOO = $null / [Environment]::SetEnvironmentVariable('FOO', [NullString]::Value) you can more clearly use Remove-Item Env:FOO in order to remove environment variable FOO.
(Note that, technically, Set-Item Env:FOO $null works too, but Set-Content Env:Foo $null does not - the latter is equivalent to Set-Content Env:Foo '', due to the -Value parameter being [object[]]-typed.)
You can't expect people to remember all those constrains and edge cases. Studding 4 pages of dosc should also not be required for such fundamental operations. Please add native PS commands Set/New/Remove-EnviroentVariables where all current and future quirks will be handled.
ALIENQuake, I agree that the behavior is conceptually obscure, but it is ultimately a .NET problem, exacerbated by PowerShell's counterintuitive conversion of $null to the empty string when binding string parameters.
However, you can alternatively use the provider cmdlets, as rhubarb-geek-nz notes, which avoids the conceptual obscurity; I've updated my previous comment accordingly (the update elaborates on rhubarb-geek-nz's comment, including some history; rhubarb-geek-nz, see the update re extending the provider cmdlets).
Providers have a builtin mechanism for providing dynamic parameters so theoretically the Environment provider could be adjusted to have a -Scope parameter on the various provider cmdlets on Windows only. This would have to be approved by the working group before accepted though.
Activity
jborean93 commentedon May 14, 2026
You avoid the reflection work by using
[NullString]::Valueto force PowerShell to use$nullinstead of an empty string for these overloads.This works when you want to provide
$nullexplicitly for any string typed argument in a method.mklement0 commentedon May 15, 2026
Let me attempt a summary that can be pointed to should similar issues get raised again:
.NET 9 introduced an intentional, breaking change that now requires passing
nullto thevalueparameter of[Environment]::SetEnvironmentVariable()in order to achieve removal of an environment variable; up to .NET 8, you could alternatively use the empty string (""), which in .NET 9+ now defines (or updates) a variable with the empty string as the value, for better cross-platform compatibility (and to fully surface the capabilities of the underlying WinAPI, which always supported empty env. vars., albeit only in-process ones, not persisted definitions - see below).scopevalues ofUserandMachine), passing the empty string (instead ofnull) in effect still results in removal: technically, the definition is then created / retained with the empty string as the value in registry, but Windows ignores such definitions when it populates a process' environment block.It's best to avoid relying on this, however, both for conceptual clarity and to make code future-proof.
PowerShell decided to surface this breaking change as-is, which landed in v7.5; the implications for PowerShell 7.5+ are:
In the context of PowerShell's namespace variable notation (which acts on in-process env. vars. only), this translates to:
$env:FOO = $nullremoves env. var.FOO$env:FOO = ''defines (or updates)FOOwith the empty string as the value.In the context of a direct .NET API call,
[NullString]::Valuemust be used to pass a truenullvalue, i.e. to effect removal.$env:FOO = $nullis[Environment]::SetEnvironmentVariable('FOO', [NullString]::Value)or, with the implied
scopeparameter value spelled out,[Environment]::SetEnvironmentVariable('FOO', [NullString]::Value, 'Process')[NullString]::Valueto pass a truenullto[string]-typed method parameters is a general requirement, due to PowerShell forcing[string]-typed values to be non-null (e.g.,'' -eq [string] $nullis$true)Note:
A simple way to avoid the conceptual obscurity stemming from having to assign a special value in order to remove an environment variable is to use the provider cmdlets on the
Env:drive - however, note that this only works on in-process env. vars.-Scopeparameter to the provider cmdlets as a dynamic parameter of the environment provider, on Windows, to allow targeting the persistent definitions.E.g., instead of
$env:FOO = $null/[Environment]::SetEnvironmentVariable('FOO', [NullString]::Value)you can more clearly useRemove-Item Env:FOOin order to remove environment variableFOO.(Note that, technically,
Set-Item Env:FOO $nullworks too, butSet-Content Env:Foo $nulldoes not - the latter is equivalent toSet-Content Env:Foo '', due to the-Valueparameter being[object[]]-typed.)ALIENQuake commentedon May 16, 2026
You can't expect people to remember all those constrains and edge cases. Studding 4 pages of dosc should also not be required for such fundamental operations. Please add native PS commands Set/New/Remove-EnviroentVariables where all current and future quirks will be handled.
mklement0 commentedon May 16, 2026
ALIENQuake, I agree that the behavior is conceptually obscure, but it is ultimately a .NET problem, exacerbated by PowerShell's counterintuitive conversion of
$nullto the empty string when bindingstringparameters.However, you can alternatively use the provider cmdlets, as rhubarb-geek-nz notes, which avoids the conceptual obscurity; I've updated my previous comment accordingly (the update elaborates on rhubarb-geek-nz's comment, including some history; rhubarb-geek-nz, see the update re extending the provider cmdlets).
jborean93 commentedon May 16, 2026
Providers have a builtin mechanism for providing dynamic parameters so theoretically the Environment provider could be adjusted to have a
-Scopeparameter on the various provider cmdlets on Windows only. This would have to be approved by the working group before accepted though.mklement0 commentedon May 16, 2026
Indeed (also mentioned in the updated comment above).
For anyone looking to pick this up by creating a feature request, PowerShell/PowerShell-RFC#92 (comment) may provide input.