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

Discuss and share scripts and script files...
Post Reply
terrytw
Posts: 65
Joined: 03 Mar 2023 03:37

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

Post 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...
Last edited by terrytw on 15 Mar 2024 13:05, edited 4 times in total.

highend
Posts: 13333
Joined: 06 Feb 2011 00:33
Location: Win Server 2022 @100%

Re: Powershell, space and nested quote

Post by highend »

Untested, with PS7.x

Code: Select all

run "pwsh -Command Start-Process Pwsh -Verb RunAs -WorkingDirectory '<curpath>'", , , 0;
One of my scripts helped you out? Please donate via Paypal

terrytw
Posts: 65
Joined: 03 Mar 2023 03:37

Re: Powershell, space and nested quote

Post 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.

highend
Posts: 13333
Joined: 06 Feb 2011 00:33
Location: Win Server 2022 @100%

Re: Powershell, space and nested quote

Post 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^^
One of my scripts helped you out? Please donate via Paypal

terrytw
Posts: 65
Joined: 03 Mar 2023 03:37

Re: Powershell, space and nested quote

Post 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.

highend
Posts: 13333
Joined: 06 Feb 2011 00:33
Location: Win Server 2022 @100%

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

Post by highend »

run "PowerShell -Command Start-Process PowerShell -verb runAs -ArgumentList """"""-NoExit"""" """"-Command"""" """"cd '<curpath>'""""""",,,0
That's called "quoting hell" :appl:
One of my scripts helped you out? Please donate via Paypal

Post Reply