PowerShell:使用变量组合路径

时间:2021-09-13 23:13:58

This must be something obvious, but I can't get this to work.

这一定是显而易见的,但我不能让它发挥作用。

I'm trying to build a variable that should contain the path to an existing file, using an environment variable ($env:programfiles(x86)). However I keep getting errors, and I fail to see why.

我正在尝试使用环境变量($ env:programfiles(x86))构建一个应包含现有文件路径的变量。但是我一直在收到错误,但我不明白为什么。

This works fine (if the file exists):

这工作正常(如果文件存在):

PS C:\> $f = "C:\Program Files (x86)" + '\sometextfile.txt'
PS C:\> $f
C:\Program Files (x86)\sometextfile.txt
PS C:\> gci $f
    Directory: C:\Program Files (x86)
Mode                LastWriteTime     Length Name
----                -------------     ------ ----
-a---        13/12/2010     14:03          0 sometextfile.txt
PS C:\>

However, this does not:

但是,这不是:

PS C:\> "$env:programfiles(x86)"
C:\Program Files(x86)
PS C:\> $f = "$env:ProgramFiles(x86)" + '\sometextfile.txt'
PS C:\> $f
C:\Program Files(x86)\sometextfile.txt
PS C:\> gci $f
Get-ChildItem : Cannot find path 'C:\Program Files(x86)\sometextfile.txt' because it does not exist.
At line:1 char:4
+ gci <<<<  $f
    + CategoryInfo          : ObjectNotFound: (C:\Program Files(x86)\sometextfile.txt:String) [Get-ChildItem], ItemNot
   FoundException
    + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetChildItemCommand

What's happening, and how to fix it?

发生了什么,以及如何解决它?

2 个解决方案

#1


21  

Here is what is going on...

这是发生了什么......

In any Windows PowerShell path empty characters or spaces need to be surrounded with a set of quotes or brackets. The PowerShell environment variable for the C:\Program Files (x86) is ${env:ProgramFiles(x86)} not $env:ProgamFiles(x86) since PowerShell needs to escape the empty spaces in the real path.

在任何Windows PowerShell路径中,空字符或空格都需要用一组引号或括号括起来。 C:\ Program Files(x86)的PowerShell环境变量是$ {env:ProgramFiles(x86)}而不是$ env:ProgamFiles(x86),因为PowerShell需要转义真实路径中的空白空间。

If you use the '${env:ProgramFiles(x86)}' explicit Environment variable, it works perfectly.

如果你使用'$ {env:ProgramFiles(x86)}'显式环境变量,它可以很好地工作。


This won't work...

这不会起作用......

PS C:\> cd "$env:programfiles(x86)" 
Set-Location : Cannot find path 'C:\Program Files(x86)' because it does not e
At line:1 char:3
+ cd <<<<  "$env:programfiles(x86)"
+ CategoryInfo          : ObjectNotFound: (C:\(x86):String)
+ FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.

or this....

或这个....

PS C:\> $env:ProgramFiles(x86)
Unexpected token '(' in expression or statement.
At line:1 char:19
+ $env:ProgramFiles( <<<< x86)
+ CategoryInfo          : ParserError: ((:String) [], Parent
+ FullyQualifiedErrorId : UnexpectedToken

But this works great....

但这很有效......

PS C:\> ${env:ProgramFiles(x86)}
C:\Program Files (x86)
PS C:\> $f = "${env:ProgramFiles(x86)}" + "\sometextfile.txt"
PS C:\> $f
C:\Program Files (x86)\sometextfile.txt
PS C:\> gci $f
Directory: C:\Program Files (x86)


Mode                LastWriteTime     Length Name
----                -------------     ------ ----
-a---        12/13/2010   8:58 AM          0 sometextfile.txt

#2


1  

That is weird. Looks like a bug. What it is actually doing though is resolving the $env:programfiles variable and appending the rest of the string - which in this case just happens to be (x86).

这很奇怪。看起来像个bug。它实际上正在做的是解析$ env:programfiles变量并追加其余的字符串 - 在这种情况下恰好是(x86)。

This will work though:

这将工作:

$f =  ${env:ProgramFiles(x86)} + '\sometextfile.txt'

#1


21  

Here is what is going on...

这是发生了什么......

In any Windows PowerShell path empty characters or spaces need to be surrounded with a set of quotes or brackets. The PowerShell environment variable for the C:\Program Files (x86) is ${env:ProgramFiles(x86)} not $env:ProgamFiles(x86) since PowerShell needs to escape the empty spaces in the real path.

在任何Windows PowerShell路径中,空字符或空格都需要用一组引号或括号括起来。 C:\ Program Files(x86)的PowerShell环境变量是$ {env:ProgramFiles(x86)}而不是$ env:ProgamFiles(x86),因为PowerShell需要转义真实路径中的空白空间。

If you use the '${env:ProgramFiles(x86)}' explicit Environment variable, it works perfectly.

如果你使用'$ {env:ProgramFiles(x86)}'显式环境变量,它可以很好地工作。


This won't work...

这不会起作用......

PS C:\> cd "$env:programfiles(x86)" 
Set-Location : Cannot find path 'C:\Program Files(x86)' because it does not e
At line:1 char:3
+ cd <<<<  "$env:programfiles(x86)"
+ CategoryInfo          : ObjectNotFound: (C:\(x86):String)
+ FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.

or this....

或这个....

PS C:\> $env:ProgramFiles(x86)
Unexpected token '(' in expression or statement.
At line:1 char:19
+ $env:ProgramFiles( <<<< x86)
+ CategoryInfo          : ParserError: ((:String) [], Parent
+ FullyQualifiedErrorId : UnexpectedToken

But this works great....

但这很有效......

PS C:\> ${env:ProgramFiles(x86)}
C:\Program Files (x86)
PS C:\> $f = "${env:ProgramFiles(x86)}" + "\sometextfile.txt"
PS C:\> $f
C:\Program Files (x86)\sometextfile.txt
PS C:\> gci $f
Directory: C:\Program Files (x86)


Mode                LastWriteTime     Length Name
----                -------------     ------ ----
-a---        12/13/2010   8:58 AM          0 sometextfile.txt

#2


1  

That is weird. Looks like a bug. What it is actually doing though is resolving the $env:programfiles variable and appending the rest of the string - which in this case just happens to be (x86).

这很奇怪。看起来像个bug。它实际上正在做的是解析$ env:programfiles变量并追加其余的字符串 - 在这种情况下恰好是(x86)。

This will work though:

这将工作:

$f =  ${env:ProgramFiles(x86)} + '\sometextfile.txt'