在 Powershell 中,在路径中输入空格时调用表达式不起作用

Bha*_*thi 5 powershell cmdlet

我有一个查询,例如当我尝试执行包含带空格的路径的表达式时,出现如下错误。

代码:

$path="E:\Test\My space\Log"
Invoke-Expression $path 

E:\Test\My: The term 'E:\test\My' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:1
+ E:\Test\My space\Log
+ ~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (E:\Test\My :String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException
Run Code Online (Sandbox Code Playgroud)

你能帮我解决这个问题吗?

iRo*_*Ron 4

使用单引号和反引号(重音符号)来转义空格:

$path='E:\Test\My` space\Log'
Invoke-Expression $path
Run Code Online (Sandbox Code Playgroud)

或者编程:

$path="E:\Test\My space\Log"
Invoke-Expression ($path -Replace ' ', '` ')
Run Code Online (Sandbox Code Playgroud)

  • 你是对的,这里可能存在一个基本的理解问题,但它确实回答了这个问题:*“你能帮我解决这个问题吗?”* (3认同)