Skip to content

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

@ikreva

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Needs-TriageThe issue is new and needs to be triaged by a work group.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions