0

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?

spanner1122
  • 106
  • 1
  • 11

1 Answers1

2

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"

Manu
  • 1,685
  • 11
  • 27
  • Perhaps I wasn't clear in the question, sorry. My issue is with spaces in the %V path, the Powershell executable is fine. I tried placing `" around %V and I still received an error. – spanner1122 Oct 19 '17 at 08:13
  • What is the exact error? Did you keep the `'` around the `" ? – Manu Oct 19 '17 at 08:21
  • I've updated my question above to include the error and the full command that I used after modifications. – spanner1122 Oct 19 '17 at 08:31
  • 1
    Take a look at this post : https://stackoverflow.com/a/24603961/7890164 – Manu Oct 19 '17 at 10:06