Based on the contribution from @Darío León - but including more secure fully qualified paths, and the Windows 'Run as a different user' dialog.
The underlying principles are as follows:
- Run Windows PowerShell hidden, then using
Start-Process -Verb RunAsUser, start another Windows PowerShell session as a different user, then sleep for 2 minutes (the default UAC timeout - if left out, the 'Run as a different user' dialog closes immediately)
- Within the second PowerShell session, using
Start-Process -Verb RunAs, run the final application elevated
This seems to do the job for me, even when the new Windows 11 'Windows Terminal' is taking over from Windows PowerShell.
Hope this helps others out:
Run Command Prompt as elevated different user
%SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe -NoProfile -WindowStyle Hidden -Command Start-Process -Verb RunAsUser $PSHOME\powershell.exe '-NoProfile -Command Start-Process -Verb RunAs \\\"%ComSpec%\\\"'; Start-Sleep 120
Run Windows PowerShell as elevated different user
%SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe -NoProfile -WindowStyle Hidden -Command Start-Process -Verb RunAsUser $PSHOME\powershell.exe '-NoProfile -Command Start-Process -Verb RunAs \\\"$PSHOME\powershell.exe\\\"'; Start-Sleep 120
Run Windows PowerShell ISE as elevated different user
%SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe -NoProfile -WindowStyle Hidden -Command Start-Process -Verb RunAsUser $PSHOME\powershell.exe '-NoProfile -Command Start-Process -Verb RunAs \\\"$PSHOME\powershell_ise.exe\\\"'; Start-Sleep 120
Run VS Code as elevated different user
%SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe -nop -w h -c Start-Process -Verb RunAsUser $PSHOME\powershell.exe '-nop -c Start-Process -Verb RunAs \\\"%ProgramFiles%\Microsoft VS Code\Code.exe\\\"'; Start-Sleep 120
The final command is shortened using the Powershell.exe command line shortcuts (-nop -w h -c) to keep it within the MAX_PATH limit (260 characters).