Home > database >  What registry commands allow me to run PowerShell 5 / CMD as admin from the right click context menu
What registry commands allow me to run PowerShell 5 / CMD as admin from the right click context menu

Time:12-16

I am trying to set up my right click context menu to launch both CMD and PowerShell5 at the current directory. My PowerShell 7 commands work fine.

This is what I got:

Context Menu

The registry command to open PowerShell5 as Admin is as follows:

PowerShell -Command "Start-Process cmd -ArgumentList '/s,/k,pushd %V && start PowerShell -NoExit && exit' -Verb RunAs"

The registry command to open the Command Prompt as Admin is as follows:

pwsh -noprofile -windowstyle hidden -Command "Start-Process cmd.exe -ArgumentList '/s,/k,pushd,%V' -Verb RunAs"

The registry command I have to open the Node command prompt is this: cmd.exe /s /k "pushd "%V" & "C:\Program Files\nodejs\nodevars.bat""

(I couldn't figure out how to open the node prompt as admin).

Today, I realized that all of these commands fail if the directory I'm opening to has an apostrophe in the path or folder name. I've tried so many different registry commands and combinations of quotes and escape characters and nothing is working. It's getting enormously frustrating.

Does anyone know of any working commands to open both CMD and PowerShell 5 in directories that have an apostrophe?

Any help at all would be greatly appreciated.

CodePudding user response:

The keys to the solution are:

  • Avoid '...' quoting altogether - use "..." quoting only.

  • Nested "..." quoting requires intricate escaping using \ as the escape character.

  • An additional trick is required to handle the case when %V expands to a root path (e.g. C:\): an argument of "C:\" would be parsed as if the \" were an escaped " rather than a verbatim \ followed by an unescaped (syntactic) ". By simply using %V/ rather than %V, this problem is bypassed (the / is effectively ignored by both cmd.exe and PowerShell).

Note:

  • The following commands are designed to be placed verbatim in the registry. Doing so programmatically complicates escaping further.

  • You can test-drive the commands as follows:

    • Replace %V with the literal path of a folder of interest.
    • Submit via the Windows Run dialog (WinKey-R); alternatively, run them from cmd.exe, but then you'll have to ^-escape metacharacters such as &.

Running powershell.exe (Windows PowerShell) with elevation (as admin):

powershell.exe -WindowStyle Hidden -NoProfile -Command "Start-Process -Verb RunAs powershell.exe -ArgumentList \"-NoExit -Command Push-Location \\\"\"%V/\\\"\"\"

Running cmd.exe with elevation (as admin):

powershell.exe -WindowStyle Hidden -NoProfile -Command "Start-Process -Verb RunAs cmd.exe -ArgumentList \"/k pushd \"\"%V/\"\"\"

Running a cmd.exe prompt with elevation (as admin) with the Node.js environment set up:

powershell.exe -WindowStyle Hidden -NoProfile -Command "Start-Process -Verb RunAs cmd.exe -ArgumentList \"/k pushd \"\"%V\"\" & \"\"C:\Program Files\nodejs\nodevars.bat\"\"\"

Note: To test from cmd.exe, use ^& instead of &.

  • Related