Powershell打开带有空格的文件路径

12

我在我的PS脚本中想要通过以下方式在另一个PS实例中运行另一个脚本:

$filepath = Resolve-Path "destruct.ps1"
    
start-process powershell.exe "$filepath"

destruct.ps1与此脚本位于同一文件夹中。

然而,当在包含空格的位置("C:\My Scripts\")运行此脚本时,将会收到以下错误:

未将'C:\My'识别为 cmdlet、函数、可运行程序或脚本文件。请检查该项的拼写。

我知道使用Invoke-Expression方法和 '&' 可解决此问题,如何使用start-process方法达到相同的效果?

8个回答

21

试一试:

 start-process -FilePath powershell.exe -ArgumentList "-file `"$filepath`""

评论后进行编辑:

start-process -FilePath powershell.exe -ArgumentList "-file `"$($filepath.path)`""

顺便提一下:

$filepath 是一个 [pathinfo] 类型而不是一个 [string] 类型。


还有一个问题!我如何使用这种方法传递参数?我有$courseName要传递给$filePath脚本,该脚本具有courseName参数。我尝试了以下内容,但它不起作用:start-process -FilePath powershell.exe -ArgumentList“-file"$($filepath.path)"-courseName $courseName" - user3402227
@user3402227 尝试发布一个新问题,对我来说似乎可以工作,但评论不适合发布代码。 - CB.

15

您可以添加转义的双引号,以便传递带引号的参数:

 "`"$filepath`""

另一个选择是在“外部”使用单引号,这样双引号将被视为字面量。 - user189198
2
然后你得到了双引号,但是$filepath不会扩展。如果你把它放在双引号内的单引号中,它就会扩展,但命令行不喜欢单引号。 - mjolinor
嘿,非常感谢你的帮助。不幸的是,这仍然没有解决问题?使用没有空格的$filepath可以工作,但有空格则不行... - user3402227
@mjolinor 嗯..这样 -file 参数就是用于 start-process cmdlet 的。 - CB.
我通常会这样做:'"{0}"' -f $FilePath - user189198
1
那是另一个选择。不管怎样,在参数列表中你都必须将文件路径用双引号括起来。 - mjolinor

2

我在这里回答一个一般性的场景。

如果你需要从PowerShell导航到一个文件夹,例如C:\Program Files,以下命令将不起作用,因为路径之间有空格。

cd C:\Program Files

相反,将路径用双引号嵌入,如下所示。

cd "C:\Program Files"


0

以防万一,如果[string]$shipno(即路径和文件名)包含空格,则以下内容允许成功将其传递给-FilePath:

if ($shipno.contains(" ") -eq $true) {
   $shipno = """" + $shipno + """"
}

0

文件名可能包含空格,因此保留完整路径中的空格:
Notepad++ 执行命令:
"C:\Windows\system32\WindowsPowerShell\v1.0\powershell.exe" "& \"$(FULL_CURRENT_PATH)\""

同样适用于命令提示符:
"C:\Windows\system32\WindowsPowerShell\v1.0\powershell.exe" "& \"C:\a_work\Systems\name with spaces.ps1\""


0

我正在运行Windows 10,Powershell版本5.1。当尝试执行PS1脚本时,我遇到了文件路径中包含空格的相同问题。在我的情况下,我有一个脚本在同一文件夹中调用另一个脚本。第一个脚本的目的是将第二个脚本提升为管理员权限。空格在计算机用户的名称中,因此他们的桌面类似于“C:\ Users \ User Space Name \ Desktop”。我尝试了互联网上的每个建议,包括这里和其他StackOverflow帖子的建议。没有一个起作用。如果我先打开PowerShell,我可以让脚本运行,但我无法通过双击文件来运行它们。我总是会得到形式为“'C:\ Users \ User'不是已识别的cmdlet...”的错误。

我发现,如果您编辑与PowerShell相关联的文件扩展名的注册表条目,则它将开始工作。注册表条目如下:

HKEY_CLASSES_ROOT\Applications\powershell.exe\shell\open\command

默认值:"C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" "%1"

新值:"C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" "& "%1""

我不确定为什么这样可以,但它确实可以。


你的回答可以通过补充支持信息来改进。请编辑以添加更多细节,例如引用或文献,以便他人可以确认您的回答是否正确。您可以在帮助中心找到有关编写良好答案的更多信息。 - Community

0
如果您在使用 PowerShell 7 (pwsh) 时遇到此问题并正在寻找解决方案,请按照以下步骤操作:
  1. 将参数列表作为单个字符串写入所有参数
  2. 用双引号括起脚本名称
  3. 用单引号和括号括起参数字符串

基本示例:

Start-Process pwsh -ArgumentList ('"path/to/my script with spaces.ps1"')

带有额外开关的示例:

Start-Process pwsh -ArgumentList ('-NoExit "path/to/my script with spaces.ps1"')

以管理员身份运行脚本的示例:

Start-Process -Verb runAs pwsh -ArgumentList ('-NoExit "path/to/my script with spaces.ps1"')

这是一个解决方案,用于解决https://github.com/PowerShell/PowerShell/issues/5576的问题。这个问题已经存在了五年以上,但仍未得到解决。


0

Start-Process简单示例

$Path="C:\Program Files (x86)\New folder"        # Path with spaces
$ScriptName='toto.ps1'                           # Script name
$ScriptArgument="-ExecutionMode $ExecutionMode"  # Script arguments 
    
# Spaces and other characters escaped

$ArgumentList="$($Path.Replace(' ','` ').replace('(','`(').replace(')','`)'))" + "\$ScriptName" + " " + $ScriptArgument
  
# Start-Process  

Start-Process -FilePath powershell.exe -ArgumentList $ArgumentList -PassThru -Wait -WindowStyle Hidden -ErrorAction SilentlyContinue  
    

使用Runas的Start-Process示例

$Path="C:\Program Files (x86)\New folder"        # Path with spaces
$ScriptName='toto.ps1'                           # Script name
$ScriptArgument="-ExecutionMode $ExecutionMode"  # Script arguments 
      
# Spaces and other characters escaped
   
$Path="$($Path.Replace(' ','` ').replace('(','`(').replace(')','`)'))" 
    
# Start-Process

$Command="-Command Set-Location $Path;$Path\$ScriptName $ScriptArgument" 
$Command="-Command Start-Process -FilePath Powershell.exe '$Command' -Verb RunAs -PassThru -Wait -WindowStyle Hidden"
      
Start-Process -FilePath powershell.exe $Command -PassThru -WindowStyle Hidden -ErrorAction SilentlyContinue 

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接