Repository navigation
Description
Prerequisites
- Write a descriptive title.
- Make sure you are able to repro it on the latest released version
- Search the existing issues.
- Refer to the FAQ.
- Refer to Differences between Windows PowerShell 5.1 and PowerShell.
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 passActual 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: 0Error 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.0Visuals
No response
Activity
[-]Passing `$null` to `[Environment]::SetEnvironmentVariable` no longer unsets environment variables (7.5 RC1)[/-][+]Passing `$null` to `[Environment]::SetEnvironmentVariable` sets environment variable to empty string `''` (7.5 RC1)[/+]pjking07 commentedon Dec 3, 2024
I believe this is related to parameter parsing for dotnet methods, in which
nullstring values seem to coalesce to an empty string"".As an example, let's use .NET string compare between null and an empty string in both PowerShell and C#. We'll find that PowerShell finds them equal while C# finds them different.
rhubarb-geek-nz commentedon Dec 3, 2024
See #24605
kilasuit commentedon Dec 3, 2024
I think that this is similar to the discussion that was had in Issue 21186 & the output from the Working Group in this comment
mklement0 commentedon Dec 3, 2024
Thanks, Ryan Yates (@kilasuit).
Indeed, this was an intentional breaking change in .NET 9, which the working group in effect decided to surface in PowerShell too:
To recap, based on #21186 (comment):
[As before]
$env:FOO = $nullwill remove the variable.[Breaking change]
$env:FOO = ''will now create it as / set it to the empty string.[Newly introduced bug]
Set-Content Env:FOO -Value $nullconverts$nullto the empty string (due the parameter binding turning the$nullinto an empty array, which is then stringified to the empty string), so it will no longer remove the variable.Set-Item Env:FOO ...behaves the same as namespace variable notation ($env:FOO = ...)microsoft-github-policy-service commentedon Dec 5, 2024
This issue has been marked as duplicate and has not had any activity for 1 day. It has been closed for housekeeping purposes.
microsoft-github-policy-service commentedon Dec 5, 2024
📣 Hey PJ King (@pjking07), how did we do? We would love to hear your feedback with the link below! 🗣️
🔗 https://aka.ms/PSRepoFeedback
Guard shim PATH entry on existence, use NullString for broadcast delete
fix: use direct registry API for PATH writes instead of SetEnvironmen…