PowerShell quotes for Start-Process ArgumentList

北城余情 提交于 2021-02-07 18:40:16

问题


I'm trying to have the app tablacus explorer open a folder path. This works fine with the following formatting:

$ex = 'S:\Tools\explorer\TE64.exe'
Start-Process $ex -ArgumentList '"Tabs,Close other tabs" "Open,C:\Program Files"'

But I would really like to have the path in a variable ($dir = 'C:\Program Files'), and I can't seem to get the quotes right so it gets interpreted properly.

Thank you for your help.


回答1:


I found two solutions for this on the MS Blog:

$Args = @"
"Tabs,Close other tabs" "Open,$dir"
"@

start $ex -ArgumentList $Args

or

start $ex -ArgumentList """Tabs,Close other tabs"" ""Open,$dir"""



回答2:


I found sometimes you need another level of quotes.

In my case, I have to set variables in -Arguments /v, so I had to use \"" to do that.

Start-Process `
  -FilePath "Installer.exe" `
  -Arguments "/s /qn /v""SOME_PARAM1=\""STRING_IN_PARAM\"" SOME_PARAM2=\""STRING_IN_PARAM\"""
  -Wait ;



回答3:


If your parameters are constant strings then create a shortcut and call that instead.

Set the 'target' of the shortcut to:

"S:\Tools\explorer\TE64.exe" "Tabs,Close other tabs" "Open,C:\Program Files"

Name your shortcut 'TE64' and call it in powershell like this:

start-process S:\Tools\explorer\TE64.lnk



回答4:


The following syntax works fine for me, try this:

-ArgumentList "\`"$($variable)\`""


来源:https://stackoverflow.com/questions/30524456/powershell-quotes-for-start-process-argumentlist

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!