简体   繁体   中英

Literal quotes in “Powershell Start-Process -ArgumentList”

I am currently attempting to create a command that opens an admin Powershell from the right click context menu. For context, context menu commands run in CMD.

My issue is that I am trying to cd into the directory where the right click occurs. The below command works just fine for most directories, but if the directory path contains a space, then it will only try to move into the portion of the path before the space, throwing an error. My understanding is that the current directory is passed in through %V but when I run the command echo %V using the same process, it splits paths with a space onto 2 lines, so I assume the parts of the path are stored in separate strings?

Powershell -noexit "Start-Process 'C:\Users\<me>\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Windows PowerShell\Windows PowerShell.lnk' -ArgumentList '-noexit','Set-Location -literalPath `"%V`"' -Verb RunAs"

I have updated the above command to match a suggestion below, and when right clicking on the desktop (which previously worked due to a lack of spaces) I now get the following error:

Set-Location : Cannot find path 'C:\Users\<me>\Desktop`' because it does not exist.
At line:1 char:1
+ Set-Location -literalPath `C:\Users\<me>\Desktop`
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : ObjectNotFound: (C:\Users\<me>\Desktop`:String) [Set-Location], ItemNotFoundException
+ FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.SetLocationCommand

Note that in both of the above code blocks, <me> is my actual username.

I've been tearing my hair out trying to put quotes around the path but I can't seem to get Powershell to put the quotes in due to the fact that I already use both single and double quotes.

Any help would be greatly appreciated, thanks in advance.

Edit:

For those still looking for an answer, I ran the following Powershell script to add functional commands to my context menu:

$menu = 'Open Windows PowerShell Here as Administrator'
$command = "$PSHOME\powershell.exe -NoExit -NoProfile -Command ""Set-Location '%V'"""

'directory', 'directory\background', 'drive' | ForEach-Object {
    New-Item -Path "Registry::HKEY_CLASSES_ROOT\$_\shell" -Name runas\command -Force |
    Set-ItemProperty -Name '(default)' -Value $command -PassThru |
    Set-ItemProperty -Path {$_.PSParentPath} -Name '(default)' -Value $menu -PassThru |
    Set-ItemProperty -Name HasLUAShield -Value ''
}

The answer was found from How do I start PowerShell from Windows Explorer?

If you want to avoid space issues, you can reuse " by escaping it with ` in a string.

For example :

$command = "Set-Location `"C:\temp\test space`""

String will become this and spaces will be handled correctly :

Set-Location "C:\\temp\\test space"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM