Title: Passing `$null` to `[Environment]::SetEnvironmentVariable` sets environment variable to empty string `''` (7.5 RC1) · Issue #24637 · PowerShell/PowerShell · GitHub
Open Graph Title: Passing `$null` to `[Environment]::SetEnvironmentVariable` sets environment variable to empty string `''` (7.5 RC1) · Issue #24637 · PowerShell/PowerShell
X Title: Passing `$null` to `[Environment]::SetEnvironmentVariable` sets environment variable to empty string `''` (7.5 RC1) · Issue #24637 · PowerShell/PowerShell
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 reprodu...
Open Graph 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 PowerSh...
X 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 PowerSh...
Opengraph URL: https://github.com/PowerShell/PowerShell/issues/24637
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Passing `$null` to `[Environment]::SetEnvironmentVariable` sets environment variable to empty string `''` (7.5 RC1)","articleBody":"### Prerequisites\r\n\r\n- [X] Write a descriptive title.\r\n- [X] Make sure you are able to repro it on the [latest released version](https://github.com/PowerShell/PowerShell/releases)\r\n- [X] Search the existing issues.\r\n- [X] Refer to the [FAQ](https://github.com/PowerShell/PowerShell/blob/master/docs/FAQ.md).\r\n- [X] Refer to [Differences between Windows PowerShell 5.1 and PowerShell](https://learn.microsoft.com/powershell/scripting/whats-new/differences-from-windows-powershell).\r\n\r\n### Steps to reproduce\r\n\r\nIn .NET 9, a [breaking change](https://learn.microsoft.com/en-us/dotnet/core/compatibility/core-libraries/9.0/empty-env-variable) 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#.\r\n\r\nPR #24133 introduced .NET 9, and the above breaking change, into PowerShell v7.5.0-preview.4.\r\n\r\nHowever, 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.\r\n\r\nThe 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`.\r\n\r\n```pwsh\r\nDescribe 'Environment Variable' {\r\n\r\n Context 'System.Environment' {\r\n It 'Should unset environment variables with null' {\r\n [System.Environment]::SetEnvironmentVariable('TEST_DOTNETAPI_NULL', $null)\r\n [System.Environment]::GetEnvironmentVariable('TEST_DOTNETAPI_NULL') | Should -Be $null\r\n }\r\n\r\n # See breaking change in .NET 9:\r\n # https://learn.microsoft.com/en-us/dotnet/core/compatibility/core-libraries/9.0/empty-env-variable\r\n It 'Should set environment variables with empty string' -Skip:([Environment]::Version.Major -lt 9) {\r\n [System.Environment]::SetEnvironmentVariable('TEST_DOTNETAPI_EMPTY', '')\r\n [System.Environment]::GetEnvironmentVariable('TEST_DOTNETAPI_EMPTY') | Should -Be ''\r\n }\r\n }\r\n\r\n Context 'ENV Drive' {\r\n It 'Should unset environment variables with null' {\r\n $ENV:TEST_ENV_NULL = $null\r\n $ENV:TEST_ENV_NULL | Should -Be $null\r\n }\r\n\r\n # See breaking change in .NET 9:\r\n # https://learn.microsoft.com/en-us/dotnet/core/compatibility/core-libraries/9.0/empty-env-variable\r\n It 'Should set environment variables with empty string' -Skip:([Environment]::Version.Major -lt 9) {\r\n $ENV:TEST_ENV_EMPTY = ''\r\n $ENV:TEST_ENV_EMPTY | Should -Be ''\r\n }\r\n }\r\n}\r\n```\r\n\r\n### Expected behavior\r\n\r\n```console\r\nAll four tests pass\r\n```\r\n\r\n\r\n### Actual behavior\r\n\r\n```console\r\n> Invoke-Pester .\\EnvironmentVariable.Tests.ps1\r\n\r\nStarting discovery in 1 files.\r\nDiscovery found 4 tests in 7ms.\r\nRunning tests.\r\n[-] Environment Variable.System.Environment.Should unset environment variables with null 4ms (3ms|0ms)\r\n Expected $null, but got \u003cempty>.\r\n at [System.Environment]::GetEnvironmentVariable('TEST_DOTNETAPI_NULL') | Should -Be $null, C:\\EnvironmentVariableTest\\EnvironmentVariable.Tests.ps1:6\r\n at \u003cScriptBlock>, C:\\EnvironmentVariableTest\\EnvironmentVariable.Tests.ps1:6\r\nTests completed in 60ms\r\nTests Passed: 3, Failed: 1, Skipped: 0, Inconclusive: 0, NotRun: 0\r\n```\r\n\r\n\r\n### Error details\r\n\r\n_No response_\r\n\r\n### Environment data\r\n\r\n```powershell\r\nName Value\r\n---- -----\r\nPSVersion 7.5.0-rc.1\r\nPSEdition Core\r\nGitCommitId 7.5.0-rc.1\r\nOS Microsoft Windows 10.0.22631\r\nPlatform Win32NT\r\nPSCompatibleVersions {1.0, 2.0, 3.0, 4.0…}\r\nPSRemotingProtocolVersion 2.3\r\nSerializationVersion 1.1.0.1\r\nWSManStackVersion 3.0\r\n```\r\n\r\n\r\n### Visuals\r\n\r\n_No response_","author":{"url":"https://github.com/pjking07","@type":"Person","name":"pjking07"},"datePublished":"2024-12-03T18:30:07Z","url":"https://github.com/PowerShell/PowerShell/issues/24637"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:068676b6-8b6c-3d88-119b-641e765060c8 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | A422:1ABC75:22B079C:306115B:6AFD35B9 |
| html-safe-nonce | 60403535028862c065fbb824f652740ae5d682c1b4edb037234b9066d82e1116 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBNDIyOjFBQkM3NToyMkIwNzlDOjMwNjExNUI6NkFGRDM1QjkiLCJ2aXNpdG9yX2lkIjoiMzE2MTcyODEzMzY1NzA4MTI3NCIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 013064e016a26bb6c0d128cdab7b8906e5bd4c8d3b456e474864c1bb3a946fdc |
| hovercard-subject-tag | issue:2715733316 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/PowerShell/PowerShell/24637/issue_layout |
| twitter:image | https://opengraph.githubassets.com/f3b27714078f9b049571b67efcd11da45d083d379a43448d21065982f83b6d4a/PowerShell/PowerShell/issues/24637 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/f3b27714078f9b049571b67efcd11da45d083d379a43448d21065982f83b6d4a/PowerShell/PowerShell/issues/24637 |
| og:image:alt | 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 PowerSh... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | pjking07 |
| hostname | github.com |
| expected-hostname | github.com |
| None | 065128366c233a9d57ff68ab6c96e25b99dd1b0ab70d23b70873d3dd6b5e4f19 |
| turbo-cache-control | no-preview |
| go-import | github.com/PowerShell/PowerShell git https://github.com/PowerShell/PowerShell.git |
| octolytics-dimension-user_id | 11524380 |
| octolytics-dimension-user_login | PowerShell |
| octolytics-dimension-repository_id | 49609581 |
| octolytics-dimension-repository_nwo | PowerShell/PowerShell |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 49609581 |
| octolytics-dimension-repository_network_root_nwo | PowerShell/PowerShell |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 941021edf0c2ec58298f4a2c8f27ae507d129c92 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width