Skip to content

[Environment]::SetEnvironmentVariable(..., $null, 'User') does not fully remove user environment variables since PowerShell 7.5 #27414

Open

Description

@ikreva

Prerequisites

Steps to reproduce

Summary

In PowerShell 7.5+ on Windows, calling:

[Environment]::SetEnvironmentVariable($Name, $null, 'User')

does not completely remove the user environment variable.

Instead, it leaves behind an environment variable entry with an empty value.

This behavior did not occur in PowerShell 7.4.14 and earlier.

The issue has persisted across multiple versions since 7.5.


Steps to Reproduce

$Name = "TEST_ENV_VAR"

# Create variable
[Environment]::SetEnvironmentVariable($Name, "123", "User")

# Remove variable
[Environment]::SetEnvironmentVariable($Name, $null, "User")

After this, inspect the user environment variables:

[Environment]::GetEnvironmentVariables("User")

or check:

  • System Properties → Environment Variables
  • Registry:
    HKEY_CURRENT_USER\Environment

Expected Behavior

The environment variable should be completely removed.

No variable named TEST_ENV_VAR should remain.

This is the behavior in PowerShell 7.4.14.


Actual Behavior

The variable name still exists, but its value becomes empty/null.

Example:

TEST_ENV_VAR=

The registry entry is not fully deleted.


Environment

  • OS: Windows

  • Affected versions:

    • PowerShell 7.5.x
    • PowerShell 7.6.1
  • Last known good version:

    • PowerShell 7.4.14

Notes

This appears to be a regression introduced in PowerShell 7.5.

The issue may affect scripts or tools that rely on fully deleting environment variables rather than setting them to empty strings.

Expected behavior

The environment variable should be completely removed.

No variable named TEST_ENV_VAR should remain.

This is the behavior in PowerShell 7.4.14.

Actual behavior

The variable name still exists, but its value becomes empty/null.

Example:

TEST_ENV_VAR=

The registry entry is not fully deleted.

Error details

Environment data

Environment
OS: Windows
Affected versions:
PowerShell 7.5.x
PowerShell 7.6.1
Last known good version:
PowerShell 7.4.14

Visuals

This appears to be a regression introduced in PowerShell 7.5.

The issue may affect scripts or tools that rely on fully deleting environment variables rather than setting them to empty strings.

Activity

  1. added
    Needs-Triage
    on May 13, 2026
  2. jborean93 commented on May 14, 2026

    @jborean93
    Collaborator

    You avoid the reflection work by using [NullString]::Value to force PowerShell to use $null instead of an empty string for these overloads.

    [Environment]::SetEnvironmentVariable('env_var', [NullString]::Value, 'User')

    This works when you want to provide $null explicitly for any string typed argument in a method.

  3. mklement0 commented on May 15, 2026

    @mklement0
    Contributor

    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 = $null removes 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.)

  4. ALIENQuake commented on May 16, 2026

    @ALIENQuake

    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.

  5. mklement0 commented on May 16, 2026

    @mklement0
    Contributor

    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).

  6. jborean93 commented on May 16, 2026

    @jborean93
    Collaborator

    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.

  7. mklement0 commented on May 16, 2026

    @mklement0
    Contributor

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Needs-Triage

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions