lit
lit

Reputation: 16266

Environment variable is not realized correctly

When I use the $Env:ProgramFiles(x86) environment variable, there is no space character before the opening parenthesis. How can I get PowerShell to produce the necessary space? PSVersion 5.0.10586.117

PS C:\src\powershell\t> "$Env:ProgramFiles(x86)"
C:\Program Files(x86)
PS C:\src\powershell\t> Get-ChildItem "$Env:ProgramFiles(x86)"
Get-ChildItem : Cannot find path 'C:\Program Files(x86)' because it does not exist.
At line:1 char:1
+ Get-ChildItem "$Env:ProgramFiles(x86)"
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (C:\Program Files(x86):String) [Get-ChildItem]
   , ItemNotFoundException
    + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetChildItemCommand

Upvotes: 1

Views: 122

Answers (1)

briantist
briantist

Reputation: 47872

It's not the space that's missing, it's that it isn't interpreting the parentheses as part of the variable name.

Use the ${name} variable syntax.

Anything in the braces is treated as part of the variable name, including spaces, punctuation, special characters, even newlines.

${env:ProgramFiles(x86)}

If you were to tab complete $env:ProgrTAB it would insert the braces for you.

Upvotes: 4

Related Questions