Our great sponsors
-
WorkOS
The modern identity platform for B2B SaaS. The APIs are flexible and easy-to-use, supporting authentication, user identity, and complex enterprise features like SSO and SCIM provisioning.
Using Start-ProcessWith with a -Credential object should work for both running in session 0 (Enter-PSSession) and using the -Wait parameter to wait for it to complete. Unfortunately it will spawn in a new window (which you can hide though). If you want it to run the new process as another user but in the same window you essentially need Start-ProcessEx with a -Token. This allows you to spawn a process in the current terminal but unfortunately the -Token side requires privileges that not even normal Administrators typically have, thus would need to be run as SYSTEM.
I use Start-Sleep here because the -Wait doesn't work with -Credential for the Start-Process - this is a known issue. See https://github.com/PowerShell/PowerShell/issues/17033
Related posts
- 3 lines of code don't understand the results.
- Task Scheduler -windowstyle hidden / minimized
- Just messing around with arrays and efficiency in PS, thought I'd share
- Register-ArgumentCompleter: how to fall back to file completion when completing a flag such as "--foo="
- New PowerShell Version - v7.3.7: [7.3.7] - 2023-09-18