I have a Windows 10 PC with an admin account and a regular user account. I want to write a Powershell script to run a program in the regular user account from the admin account. So I would run the script from the Admin account and the UI pops up in the user account. The user account would be logged in at the same time as the admin. I tried using psexec.exe but the UI comes up blank and unresponsive in the user account. Is it possible? If so, how do I do it? Thanks

1 Answer 1

start-process powershell -Credential $user

Where https://serverfault.com/questions/95431/in-a-powershell-script-how-can-i-check-if-im-running-with-administrator-privil shows how to check if you are admin to confirm this:

$currentPrincipal = New-Object Security.Principal.WindowsPrincipal([Security.Principal.WindowsIdentity]::GetCurrent())

So I launched Powershell as Administrator, used this to check it is Administrator then after launching the other Powershell process from that one confirmed that one was not in Administrator mode.

That should do what you need.

If you want to avoid the password prompt it is possible to generate a credential object to use in the credential parameter instead. I would suggest checking the options here: https://techcommunity.microsoft.com/t5/office-365/cred-get-credential-without-asking-for-prompts-in-powershell/m-p/483274

  • 1
    Though in the last link, this is possible, you are still passing plain text credentials [$password = ConvertTo-SecureString "mypassword" -AsPlainText -Force] in a script and that is not prudent. Just don't do this. Always prompt for creds (Get-Credential), at least once, and then if they need t to be reused, then store those secure creds in the Windows credential manager or the registry. There are many articles/videos on secure password use and storage all over the web.
    – postanote
    Dec 20, 2020 at 22:48
  • That link does contain those options as well as the simplest use case - I would expect that during development you would presumably use a test user with known password to confirm script operation before changing the password management setup but that's for the OP to figure out.
    – cyborg
    Dec 21, 2020 at 17:18

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .