Page 1 of 1

[Solved] Powershell, space and nested quote (right click menu to open powershell here with admin privilege)

Posted: 15 Mar 2024 07:46
by terrytw
Hi guys I run into a problem with nested quotes, can someone please help me out?

I have got a script like this:

Code: Select all

run "PowerShell -Command Start-Process PowerShell -verb runAs -ArgumentList '-NoExit', '-Command', 'cd <curpath>'",,,0
and it runs, except when there is a space in <curpath>...
But I don't know how to wrap it up with quotes, have tried some different approaches to no avail...

Thanks!

EDIT: I'm a stubborn guy who bangs my head over this for too long, a cautionary tale, but I did figure it out:

Code: Select all

run "PowerShell -Command Start-Process PowerShell -verb runAs -ArgumentList """"""-NoExit"""" """"-Command"""" """"cd '<curpath>'""""""",,,0
Yes you read it correctly, that is 7 double quotes in a row...

Re: Powershell, space and nested quote

Posted: 15 Mar 2024 08:51
by highend
Untested, with PS7.x

Code: Select all

run "pwsh -Command Start-Process Pwsh -Verb RunAs -WorkingDirectory '<curpath>'", , , 0;

Re: Powershell, space and nested quote

Posted: 15 Mar 2024 09:00
by terrytw
highend wrote: 15 Mar 2024 08:51 Untested, with PS7.x

Code: Select all

run "pwsh -Command Start-Process Pwsh -Verb RunAs -WorkingDirectory '<curpath>'", , , 0;
Thanks! I don't want to use pwsh, for compatibility reasons. Some machines don't have pwsh. I would like to stick with powershell 5.0.

Re: Powershell, space and nested quote

Posted: 15 Mar 2024 09:15
by highend
For those old versions I use this:

http://code.kliu.org/misc/elevate/

Code: Select all

    $elevate = "D:\Tools\@Command Line Tools\Elevate\elevate_x64.exe";
    $command = "powershell.exe -NoProfile -NoExit -Command Set-Location -LiteralPath '<curpath>'";
    run lax("$elevate" $command);
Never had the time or motivation to fiddle with this pesky ps quoting system^^

Re: Powershell, space and nested quote

Posted: 15 Mar 2024 09:30
by terrytw
highend wrote: 15 Mar 2024 09:15 For those old versions I use this:

http://code.kliu.org/misc/elevate/

Code: Select all

    $elevate = "D:\Tools\@Command Line Tools\Elevate\elevate_x64.exe";
    $command = "powershell.exe -NoProfile -NoExit -Command Set-Location -LiteralPath '<curpath>'";
    run lax("$elevate" $command);
Never had the time or motivation to fiddle with this pesky ps quoting system^^
Thanks again! I will take a look at elevate.

Re: [Solved] Powershell, space and nested quote (right click menu to open powershell here with admin privilege)

Posted: 15 Mar 2024 21:26
by highend
run "PowerShell -Command Start-Process PowerShell -verb runAs -ArgumentList """"""-NoExit"""" """"-Command"""" """"cd '<curpath>'""""""",,,0
That's called "quoting hell" :appl: