Skip to content

[Environment]::SetEnvironmentVariable() fixes and improvements #26725

Closed

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

  1. added
    Issue-Enhancement
    Needs-Triage
    on Jan 26, 2026
  2. mklement0 commented on Jan 27, 2026

    @mklement0
    Contributor

    Re 1:

    For better or - arguably - worse, PowerShell has historically translated $null to 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 $null values 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]::Value as an - awkward - way to explicitly signal the intent to pass a bona fide null.

    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):

    GitHub
    .NET is a cross-platform runtime for cloud, mobile, desktop, and IoT apps. - dotnet/runtime
  3. AkitoTheGambler commented on Jan 27, 2026

    @AkitoTheGambler
    Author

    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

  4. added
    .NET
    Resolution-Answered
    and removed
    Needs-Triage
    on Jan 27, 2026
  5. SeeminglyScience commented on Jan 27, 2026

    @SeeminglyScience
    Contributor

    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

    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

  6. kilasuit commented on Jan 27, 2026

    @kilasuit
    Collaborator

    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

  7. kilasuit commented on Jan 28, 2026

    @kilasuit
    Collaborator

    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.

  8. microsoft-github-policy-service commented on Jan 29, 2026

    @microsoft-github-policy-service
    Contributor

    This issue has been marked as answered and has not had any activity for 1 day. It has been closed for housekeeping purposes.

  9. AkitoTheGambler commented on Sep 22, 2026

    @AkitoTheGambler
    Author

    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)

  10. mklement0 commented on Sep 22, 2026

    @mklement0
    Contributor

    Thanks, Christian Gambale (@AkitoTheGambler): those functions look handy, but note that they call a non-built-in cmdlet, Get-RegistryKey

    As for adding such functionality to PowerShell: I would personally welcome that, but past discussions have stalled; see

  11. AkitoTheGambler commented on Oct 31, 2026

    @AkitoTheGambler
    Author

    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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    .NETIssue-EnhancementResolution-Answered

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions