Powershell - 从带空格的路径运行命令

时间:2015-03-18 10:08:45

标签: powershell

我试图从存储在变量中的路径执行进程(变量的值是Get-Location命令的结果)。 仅当目录没有空格时才有效。 我找到了很多方法来挖掘空间,但只有在手动分配路径时才能使用。 还有其他办法吗?

这是代码:

$userCred = Get-Credential "Domain\$admAccount"
$currentDir = Get-Location
Start-Process "pwd_user.exe" -WorkingDirectory $currentDir -Credential $userCred

我试过了:

"$currentDir"
$currentDir -replace ' ', '` '
&$currentDir
"&$currentDir"

这些都不起作用。

1 个答案:

答案 0 :(得分:0)

WorkingDirectory的默认值是当前目录。我不明白为什么你首先需要这个变量。至于变量,我想你可以将Path-property扩展为一个镜头。

Start-Process "pwd_user.exe" -WorkingDirectory $currentDir.Path -Credential $userCred

对于不成功的替换,您需要扩展属性;

$currentDir.Path -replace ' ', '` '