Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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].
Expand Down Expand Up @@ -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].

Expand All @@ -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].

Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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].
Expand Down Expand Up @@ -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].

Expand All @@ -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].

Expand Down
Loading