在 PowerShell 中组合一个带空格的路径以及一个变量和参数

Combine in PowerShell a path with spaces and a variable and parameters

我有一个 PowerShell 脚本,当 exe 的路径没有空格但不使用空格时,它可以工作。我该如何解决这个问题?

$dir = "path/to/directory"
$images = Get-ChildItem $dir
foreach ($img in $images) {
  $outputName = $img.DirectoryName + "\" + $img.BaseName + ".webp"

  ##### The line below works well when there are no spaces
  ##### C:\webp-converter\libwebp-0.6.1-windows-x64\bin\cwebp.exe $img.FullName -o $outputName

  ##### How do i change the syntax to make the line below work?
  C:\Program Files\a folder with many spaces in the title\bin\cwebp.exe $img.FullName -o $outputName
}

使用 & 调用运算符:

& 'C:\Program Files\a folder with many spaces in the title\bin\cwebp.exe' $img.FullName '-o' $outputName

或变量中的可执行文件路径:

$dir = "path/to/directory"
$images = Get-ChildItem $dir
$exe = "C:\Program Files\a folder with many spaces in the title\bin\cwebp.exe"
foreach ($img in $images) {
  $outputName = Join-Path $img.DirectoryName ($img.BaseName + ".webp")

  & $exe $img.FullName '-o' $outputName
}