Run PowerShell script as a different user and elevated

Solution 1:

Are you automating something or just running a script occasionally? Is the script directory local or on the network?

As you've noticed, starting a new instance of powershell with runas won't change the user, and runasuser won't elevate the process. You'll need to do them both in the opposite order. If you are logged in as the local admin, start Powershell with RunAsUser, or through:

  • Shift+Right-click > Run as different user > Domain admin

Then do your runas to elevate from there (as the domain admin):

Start-Process PowerShell -Verb RunAs

You can check what user you're currently running as with whoami. the result should be your domain account, even when elevated.

OR

if you are managing a PC remotely and using powershell already, connect using powershell instead as the session will always be elevated:

Enter-PSSession MyPCName -credential (get-credential -username domain\MyAdmin)
# remote session:
[MyPCName]: PS C:\WINDOWS\system32>

I also have to recommend never using the local admin account if possible.