I'm trying to set an environment variable, in PowerShell, to an empty string. Note that I don't want to unset the environment variable; I want it set but empty. However, PowerShell removes the environment variable entirely if you just set it to the empty string. I've tried the following:
$ENV:VAR=''
$ENV:VAR=[String]::Empty
$ENV:VAR=[NullString]::Value
$ENV:VAR=$null
New-Item -Path ENV:VAR -Value ''
New-Item -Path ENV:VAR -Value [string::Empty]
Each of those (except the last one) caused the variable not to appear in the output of Get-ChildItem -path ENV:. The last one set the environment variable to the literal string [string::Empty], which is definitely not what I wanted.
For completeness:
$PSVersionTable.PSVersion
Major Minor Build Revision ----- ----- ----- -------- 5 0 10586 117
I can see that it's possible to have an empty environment variable, because I have several:
ConEmuANSI ON
ConEmuAnsiLog
ConEmuArgs
ConEmuArgs2
ConEmuBackHWND 0x00F40BFC
Just for reference, I'm actually trying to set the DOCKER_TLS_VERIFY environment variable to an empty string, because docker-compose treats the absence of that variable as indicating that I want to do TLS verification, and the Docker host I'm using has TLS disabled. I've temporarily given up and switched shells.
I have found the same problem. It seems that there is a special behaviour of environment variable that they are deleted by setting them to a blank string.
This works to set a local variable to a blank string:
$p = [string]$null
$p.getType()
However if you try the same thing using an environment variable, it won't work:
$Env:p = [string]$null
$Env:p.getType()
According to this web site (https://ss64.com/ps/syntax-env.html) setting an environment variable to a blank string is the prescribed way to delete it.
I think this might be behaviour inherited from the old DOS way of doing environment variables.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With