diff --git a/reference/7.5/Microsoft.PowerShell.Core/About/about_Environment_Variables.md b/reference/7.5/Microsoft.PowerShell.Core/About/about_Environment_Variables.md index de769b6eb00..447970ac413 100644 --- a/reference/7.5/Microsoft.PowerShell.Core/About/about_Environment_Variables.md +++ b/reference/7.5/Microsoft.PowerShell.Core/About/about_Environment_Variables.md @@ -1,7 +1,7 @@ --- description: Describes how to access and manage environment variables in PowerShell. Locale: en-US -ms.date: 09/05/2024 +ms.date: 08/27/2025 online version: https://learn.microsoft.com/powershell/module/microsoft.powershell.core/about/about_environment_variables?view=powershell-7.5&WT.mc_id=ps-gethelp schema: 2.0.0 title: about_Environment_Variables @@ -122,38 +122,24 @@ The 'Foo' environment variable is set to: An example An example! ``` -In PowerShell, an environment variable can't be set to an empty string. Setting -an environment variable to `$null` or an empty string removes it from the -current session. For example: +Beginning in PowerShell 7.5, you can set an environment variable to an empty string. Setting an +environment variable to `$null` removes it from the current session. For example: ```powershell -$Env:Foo = '' -$Env:Foo | Get-Member -MemberType Properties -``` +PS> $env:TEST = '' +PS> Get-ChildItem env:TEST -```Output -Get-Member : You must specify an object for the Get-Member cmdlet. -At line:1 char:12 -+ $Env:foo | Get-Member -+ ~~~~~~~~~~ - + CategoryInfo : CloseError: (:) [Get-Member], InvalidOperationException - + FullyQualifiedErrorId : NoObjectInGetMember,Microsoft.PowerShell.Commands.GetMemberCommand -``` - -`Get-Member` returned an error because the environment variable was removed. -You can see that it doesn't return an error when you use it on an empty -string: +Name Value +---- ----- +TEST -```powershell -'' | Get-Member -MemberType Properties -``` +PS> $env:TEST = $null +PS> $env:TEST.Length +0 -```Output - TypeName: System.String +PS> Get-ChildItem env:TEST -Name MemberType Definition ----- ---------- ---------- -Length Property int Length {get;} +Get-ChildItem: Cannot find path 'TEST' because it does not exist. ``` For more information about variables in PowerShell, see [about_Variables][11]. @@ -208,6 +194,31 @@ Use the `Get-ChildItem` cmdlet to see a full list of environment variables: Get-ChildItem Env: ``` +Beginning in PowerShell 7.5, you can set an environment variable to an empty +string using the environment provider and `Set-Item` cmdlet. Setting an +environment variable to `$null` removes it from the current session. For +example: + +```powershell +PS> Set-Item env:TEST 'Foo' +PS> Get-ChildItem env:TEST + +Name Value +---- ----- +TEST Foo + +PS> Set-Item env:TEST '' +PS> Get-ChildItem env:TEST + +Name Value +---- ----- +TEST + +PS> Set-Item -Path env:TEST -Value $null +PS> Get-ChildItem env:TEST +Get-ChildItem: Cannot find path 'TEST' because it does not exist. +``` + For more information on using the **Environment** provider to manage environment variables, see [about_Environment_Provider][04]. @@ -228,19 +239,45 @@ of `Bar` and then returns its value. Bar ``` -You can remove an environment variable with the `SetEnvironmentVariable()` -method by specifying an empty string for the variable's value. For example, -to remove the `Foo` environment variable: +Beginning in PowerShell 7.5, you can set an environment variable to an empty +string using the `SetEnvironmentVariable()` method and specifying an empty +string or `$null` for the variable's value. For example: ```powershell -[Environment]::SetEnvironmentVariable('Foo','') -[Environment]::GetEnvironmentVariable('Foo') -``` +PS> [Environment]::SetEnvironmentVariable('Foo','Bar') +PS> Get-ChildItem env:Foo -```Output +Name Value +---- ----- +Foo Bar + +PS> [Environment]::SetEnvironmentVariable('Foo','') +PS> Get-ChildItem env:Foo + +Name Value +---- ----- +Foo + +PS> [Environment]::SetEnvironmentVariable('Foo','bar') +PS> Get-ChildItem env:Foo + +Name Value +---- ----- +Foo bar +PS> [Environment]::SetEnvironmentVariable('Foo',$null) +PS> Get-ChildItem env:Foo + +Name Value +---- ----- +Foo ``` +> [!NOTE] +> Unlike the variable syntax and provider cases, assigning the value to `$null` +> using the `SetEnvironmentVariable()` method doesn't remove the environment +> variable. + For more information about the methods of the **System.Environment** class, see [Environment Methods][01]. diff --git a/reference/7.6/Microsoft.PowerShell.Core/About/about_Environment_Variables.md b/reference/7.6/Microsoft.PowerShell.Core/About/about_Environment_Variables.md index dd620bc49db..65b6a8a3738 100644 --- a/reference/7.6/Microsoft.PowerShell.Core/About/about_Environment_Variables.md +++ b/reference/7.6/Microsoft.PowerShell.Core/About/about_Environment_Variables.md @@ -1,7 +1,7 @@ --- description: Describes how to access and manage environment variables in PowerShell. Locale: en-US -ms.date: 09/05/2024 +ms.date: 08/27/2025 online version: https://learn.microsoft.com/powershell/module/microsoft.powershell.core/about/about_environment_variables?view=powershell-7.6&WT.mc_id=ps-gethelp schema: 2.0.0 title: about_Environment_Variables @@ -122,38 +122,24 @@ The 'Foo' environment variable is set to: An example An example! ``` -In PowerShell, an environment variable can't be set to an empty string. Setting -an environment variable to `$null` or an empty string removes it from the -current session. For example: +Beginning in PowerShell 7.5, you can set an environment variable to an empty string. Setting an +environment variable to `$null` removes it from the current session. For example: ```powershell -$Env:Foo = '' -$Env:Foo | Get-Member -MemberType Properties -``` +PS> $env:TEST = '' +PS> Get-ChildItem env:TEST -```Output -Get-Member : You must specify an object for the Get-Member cmdlet. -At line:1 char:12 -+ $Env:foo | Get-Member -+ ~~~~~~~~~~ - + CategoryInfo : CloseError: (:) [Get-Member], InvalidOperationException - + FullyQualifiedErrorId : NoObjectInGetMember,Microsoft.PowerShell.Commands.GetMemberCommand -``` - -`Get-Member` returned an error because the environment variable was removed. -You can see that it doesn't return an error when you use it on an empty -string: +Name Value +---- ----- +TEST -```powershell -'' | Get-Member -MemberType Properties -``` +PS> $env:TEST = $null +PS> $env:TEST.Length +0 -```Output - TypeName: System.String +PS> Get-ChildItem env:TEST -Name MemberType Definition ----- ---------- ---------- -Length Property int Length {get;} +Get-ChildItem: Cannot find path 'TEST' because it does not exist. ``` For more information about variables in PowerShell, see [about_Variables][11]. @@ -208,6 +194,31 @@ Use the `Get-ChildItem` cmdlet to see a full list of environment variables: Get-ChildItem Env: ``` +Beginning in PowerShell 7.5, you can set an environment variable to an empty +string using the environment provider and `Set-Item` cmdlet. Setting an +environment variable to `$null` removes it from the current session. For +example: + +```powershell +PS> Set-Item env:TEST 'Foo' +PS> Get-ChildItem env:TEST + +Name Value +---- ----- +TEST Foo + +PS> Set-Item env:TEST '' +PS> Get-ChildItem env:TEST + +Name Value +---- ----- +TEST + +PS> Set-Item -Path env:TEST -Value $null +PS> Get-ChildItem env:TEST +Get-ChildItem: Cannot find path 'TEST' because it does not exist. +``` + For more information on using the **Environment** provider to manage environment variables, see [about_Environment_Provider][04]. @@ -228,19 +239,45 @@ of `Bar` and then returns its value. Bar ``` -You can remove an environment variable with the `SetEnvironmentVariable()` -method by specifying an empty string for the variable's value. For example, -to remove the `Foo` environment variable: +Beginning in PowerShell 7.5, you can set an environment variable to an empty +string using the `SetEnvironmentVariable()` method and specifying an empty +string or `$null` for the variable's value. For example: ```powershell -[Environment]::SetEnvironmentVariable('Foo','') -[Environment]::GetEnvironmentVariable('Foo') -``` +PS> [Environment]::SetEnvironmentVariable('Foo','Bar') +PS> Get-ChildItem env:Foo -```Output +Name Value +---- ----- +Foo Bar + +PS> [Environment]::SetEnvironmentVariable('Foo','') +PS> Get-ChildItem env:Foo + +Name Value +---- ----- +Foo + +PS> [Environment]::SetEnvironmentVariable('Foo','bar') +PS> Get-ChildItem env:Foo + +Name Value +---- ----- +Foo bar +PS> [Environment]::SetEnvironmentVariable('Foo',$null) +PS> Get-ChildItem env:Foo + +Name Value +---- ----- +Foo ``` +> [!NOTE] +> Unlike the variable syntax and provider cases, assigning the value to `$null` +> using the `SetEnvironmentVariable()` method doesn't remove the environment +> variable. + For more information about the methods of the **System.Environment** class, see [Environment Methods][01].