Repository navigation
Description
Summary of the new feature / enhancement
[Environment]::SetEnvironmentVariable("Variable", $null)
should delete the environmentvariable
and when passing [Environment]::SetEnvironmentVariable("Variable", "")
it keeps the current behaviour of setting an empty variable
[Environment]::GetEnvironmentVariable() add a new optional parameter that can be used to get a variable not expanded
This allows to save it to a variable and pass it directly to SetEnvironmentVariable without breaking %% inside variables
also for SetEnvironmentVariable it should detect if a % is inside the parameter and set the korrekt registrykey type when saving (reg_expand_sz) currently it always saves as reg_sz which breaks Path variable when used
Proposed technical implementation details (optional)
https://stackoverflow.com/questions/69965728/cannot-remove-powershell-environment-variables
here the current behaviour is described.
and for point 3 line 90 in file https://github.com/microsoft/PowerToys/blob/main/src/modules/EnvironmentVariables/EnvironmentVariablesUILib/Helpers/EnvironmentVariablesHelper.cs
Can the powershell command be adjusted so that when passing
[Environment]::SetEnvironmentVariable("Variable", $null)
it should pass [NullString]::Value
and when passing [Environment]::SetEnvironmentVariable("Variable", '')
it keeps the current behaviour of empty.string
the current usage of
using [Environment]::SetEnvironmentVariable("Variable", [NullString]::Value)
is not very convinient or intuitive
currently this is how we can get env variables
[Environment]::GetEnvironmentVariable($Name, $Target)
we can expand this to be
[Environment]::GetEnvironmentVariable($Name, $Target, $DoNotExpand)
which toggles this behaviour
If the value passed to SetEnvironmentVariable($Name, $Value, $Target) contains "%" it should save as reg_expand_sz else reg_sz
this is already the implemented behaviour for EnvironmentVariable Editor inside PowerToys
and writing the registry directly because of this issue with SetEnvironmentVariable and unable to get Env variable unexpanded with GetEnvironmentVariable
Activity
mklement0 commentedon Jan 27, 2026
Re 1:
For better or - arguably - worse, PowerShell has historically translated
$nullto the empty string ('') in .NET API method calls to[string]-typed parameters, presumably based on the fact that[string]-typed values in PowerShell code normally don't support$nullvalues and convert them to the empty string (e.g.,[string] $null -eq ''is true, showing the implicit to-empty-string conversion) .v3 therefore introduced
[NullString]::Valueas an - awkward - way to explicitly signal the intent to pass a bona fidenull.Changing this now would amount to a nontrivial breaking change.
Re 2 and 3:
Such improvements would apply to .NET APIs, and should therefore be submitted at (assuming there are no preexisting issues to that effect):
AkitoTheGambler commentedon Jan 27, 2026
Didn't passing $null specifically for setenvironmentvariable in pre .Net 9 already work?
Im not sure if other .net commands run in powershell it was also like that or Setenvironmentvariable was an exception
SeeminglyScience commentedon Jan 27, 2026
Yeah, it was a change in .NET that made the distinction between an empty string and null. Previously if you attempted to set an environment variable to an empty string it would remove it as you expect, but it was not possible to set an environment variable to be an empty string. Not something PowerShell has control over though
kilasuit commentedon Jan 27, 2026
Please see this comment in issue 24637 that links back to this Working Group comment as there were changes in .NET for this
This is calling .NET api's and as such we don't have any control on how this works and we wouldn't likely add additional syntatical sugar here in PowerShell when it's a dotnet area that we build upon.
What you probably want (even if it feels right to call $null to remove it) is to ask dotnet to add a RemoveEnvironmentVariable method for your number 1 improvement & as Michael Klement (@mklement0) mentions 2 & 3 are also requests to extend the methods in dotnet
kilasuit commentedon Jan 28, 2026
I'd raise the issue for a RemoveEnvironmentVariable method in dotnet/runtime but the template for New API Suggestion is too much for me at this time to go through so if someone else could that be grand.
microsoft-github-policy-service commentedon Jan 29, 2026
This issue has been marked as answered and has not had any activity for 1 day. It has been closed for housekeeping purposes.
AkitoTheGambler commentedon Sep 22, 2026
Michael Klement (@mklement0) Patrick Meinecke (@SeeminglyScience) Ryan Yates (@kilasuit)
I wrote a PowerShell function as a workaround for setting environment variables, idk if maybe instead a new function can be added to PS directly for setting and removing environment variables avoiding the bugs with .NET implementation
Check the code section in the comment dotnet/runtime#132366 (comment)
mklement0 commentedon Sep 22, 2026
Thanks, Christian Gambale (@AkitoTheGambler): those functions look handy, but note that they call a non-built-in cmdlet,
Get-RegistryKeyAs for adding such functionality to PowerShell: I would personally welcome that, but past discussions have stalled; see
AkitoTheGambler commentedon Oct 31, 2026
Michael Klement (@mklement0) the functions were built for PSADT so you'd have to change the Get-Registrykey to standard powershell commands.
This was just for showing my workaround in powershell because of the broken .NET commands and on how the .NET commands should be changed so no breaking changes occur with existing scripts using the .NET commands.
For PowerShell this could be used with little changes (after switching Get-RegistryKey to standard PS with Get-ItemProperty)
Maybe it can be looked at again since this implementation i think is a pretty simple extension for PS that also works around currently existing issues when setting environment variable via script rn compared to the other method mentioned in your linked RFC.
Theres currently no easy way to set environment variables via script because the .NET commands are bugged. And i saw issues because of these bugs and no easy way in PS, keep getting opened every few months
I can convert the PSADT commands to Standard PS commands if it will be discussed again for potential inclusion