3

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.

7
  • What problem does this solve (why is it needed)? Commented Apr 27, 2018 at 14:21
  • Not sure what Docker will make of it, but you can set it to an 'empty' string by setting it to a non-printing character. For example, a 'tab': Set-Item -Path ENV:VAR -Value "`t" Commented Apr 27, 2018 at 14:21
  • @boxdog that just confuses Docker, unfortunately: Traceback (most recent call last): <snip> docker.errors.TLSParameterError: Path to a certificate and key files must be provided through the client_config param. TLS configurations should map the Docker CLI client configurations. See docs.docker.com/engine/articles/https for API details. Thanks for the suggestion, though Commented Apr 27, 2018 at 14:30
  • Try This: [String[]]$ENV:VAR = $Null or [String[]]$ENV:VAR = "" Commented Apr 27, 2018 at 14:31
  • Also, have you just tried setting DOCKER_TLS_VERIFY = $False Commented Apr 27, 2018 at 14:33

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

Comments

0

You should check out the following post on another site. From what it says, Docker ignores the value DOCKER_TLS_VERIFY and uses its presence itself as a sort of a on/off switch. If you have it in your script, it's turned on, and if you remove it, DLS is disabled.

https://github.com/moby/moby/issues/22411

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.