Passing `$null` to `[Environment]::SetEnvironmentVariable` sets environment variable to empty string `''` (7.5 RC1) · Issue #24637 · PowerShell/PowerShell · GitHub
Skip to content

Passing $null to [Environment]::SetEnvironmentVariable sets environment variable to empty string '' (7.5 RC1) #24637

@pjking07

Description

@pjking07

Prerequisites

Steps to reproduce

In .NET 9, a breaking change introduced empty string support to environment variables. As of that change, the only way to remove an environment variable is to set it to null via Environment.SetEnvironmentVariable(). I've confirmed this behavior in .NET 9 with C#.

PR #24133 introduced .NET 9, and the above breaking change, into PowerShell v7.5.0-preview.4.

However, setting an environment variable to $null in [Environment]::SetEnvironmentVariable() now sets the environment variable to an empty string, instead of the expected behavior of unsetting the environment variable. Notably, setting the variable to null via the ENV provider works as expected.

The below tests explain the expected behavior, but Environment Variable.System.Environment.Should unset environment variables with null fails because [Environment]::GetEnvironmentVariable() yields an empty string '' instead of $null.

Describe 'Environment Variable' {

    Context 'System.Environment' {
        It 'Should unset environment variables with null' {
            [System.Environment]::SetEnvironmentVariable('TEST_DOTNETAPI_NULL', $null)
            [System.Environment]::GetEnvironmentVariable('TEST_DOTNETAPI_NULL') | Should -Be $null
        }

        # See breaking change in .NET 9:
        # https://learn.microsoft.com/en-us/dotnet/core/compatibility/core-libraries/9.0/empty-env-variable
        It 'Should set environment variables with empty string' -Skip:([Environment]::Version.Major -lt 9) {
            [System.Environment]::SetEnvironmentVariable('TEST_DOTNETAPI_EMPTY', '')
            [System.Environment]::GetEnvironmentVariable('TEST_DOTNETAPI_EMPTY') | Should -Be ''
        }
    }

    Context 'ENV Drive' {
        It 'Should unset environment variables with null' {
            $ENV:TEST_ENV_NULL = $null
            $ENV:TEST_ENV_NULL | Should -Be $null
        }

        # See breaking change in .NET 9:
        # https://learn.microsoft.com/en-us/dotnet/core/compatibility/core-libraries/9.0/empty-env-variable
        It 'Should set environment variables with empty string' -Skip:([Environment]::Version.Major -lt 9) {
            $ENV:TEST_ENV_EMPTY = ''
            $ENV:TEST_ENV_EMPTY | Should -Be ''
        }
    }
}

Expected behavior

All four tests pass

Actual behavior

> Invoke-Pester .\EnvironmentVariable.Tests.ps1

Starting discovery in 1 files.
Discovery found 4 tests in 7ms.
Running tests.
[-] Environment Variable.System.Environment.Should unset environment variables with null 4ms (3ms|0ms)
 Expected $null, but got <empty>.
 at [System.Environment]::GetEnvironmentVariable('TEST_DOTNETAPI_NULL') | Should -Be $null, C:\EnvironmentVariableTest\EnvironmentVariable.Tests.ps1:6
 at <ScriptBlock>, C:\EnvironmentVariableTest\EnvironmentVariable.Tests.ps1:6
Tests completed in 60ms
Tests Passed: 3, Failed: 1, Skipped: 0, Inconclusive: 0, NotRun: 0

Error details

No response

Environment data

Name                           Value
----                           -----
PSVersion                      7.5.0-rc.1
PSEdition                      Core
GitCommitId                    7.5.0-rc.1
OS                             Microsoft Windows 10.0.22631
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

Visuals

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

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

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions