This PS1 File
(test.ps1
), while executed, will run The Commands Pushd $Home; bcdedit /enum bootmgr; pause
As Admin in PWSH.exe
its self.
# test.ps1
$AL = "-NoProfile -WindowStyle Maximized -ExecutionPolicy Bypass",
"-Command", "Pushd $Home; bcdedit /enum bootmgr; pause"
Start -Verb RunAs -FilePath pwsh.exe -ArgumentList $AL
What is the writing format, if I execute it (test.ps1
), then will opened in Windows Terminal Elevated/ As Admin?
I tried:
# test.ps1
$AL = "-M -p 'PowerShell'",
"pwsh.exe -ExecutionPolicy Bypass -Command `"Pushd $Home; bcdedit /enum bootmgr; pause`""
Start -Verb RunAs -FilePath wt.exe -ArgumentList $AL
giving the errors:
[error 2147942402 (0x80070002) when launching `" pause"']
[error 2147942402 (0x80070002) when launching `" bcdedit /enum bootmgr"']
I have also tried other formats but the error still persists.
Only works if one command:
# test.ps1
$AL = "-M -p 'PowerShell'",
"pwsh.exe -ExecutionPolicy Bypass -Command `"bcdedit /enum bootmgr`""
Start -Verb RunAs -FilePath wt.exe -ArgumentList $AL
Best Answer
Unescaped
;
on thewt.exe
command line serve to separate multiple commands for Windows Terminal.Therefore,
;
characters to be passed through to a target executable must be escaped, namely as\;
, which is unfortunately always necessary - even for;
characters inside (embedded)"..."
.Therefore:
Note:
wt.exe
's-d
option to pass a startup directory.-ArgumentList
parameter, which is generally preferable to work around a long-standing bug that won't be fixed - see this SO answer.