Running cmdlets as another user


I need some help on using the best way to do something.

I’m working on a script. The script will be launched using a non admin user. The script will run some cmdlets (Microsoft Configuration Manager). I need to run them using another user. What would be the best solution to do it? The CM cmdlets don’t provide the option to specify the user to use. From what I read, using invoke-command is not possible neither since it require to provide another computername and localhost won’t work.

Thanks for your help




If you have SCCM in place you don’t need to switch accounts/credentials inside scripts. You should specify in SCCM to run the script with administrative rights.

That script will be ran by users who don’t have enough rights within our SCCM. And we don’t want to give them more. Those cmdlets need to run using a service account that will have enough rights to perform the task. That’s why I’m doing it that way.


That script will be ran by users who don't have enough rights within our SCCM.
You are running script(s) that must be ran in the context currently logged on user which also requires admin privs due to the SCCM priv requirement?

Since you are saying you cannot follow Olaf’s recommendation, IMHO (taking the interactive user out of the picture) your option(s): (1) is to create a scheduled task remotely, and have that ST run the scripts using the creds you provide, and only set to run when the user is logged on. (2) Use MS SysInternals psexec with the credentials needed to execute what you are after.

PS, does not provide native a way for you to run scripts logged on user context, as this is a Windows security boundary.

I’ve done this in the past as @postanote recommended, which is basically setup a queue that has a scheduled task running as the privileged account. You drop Computer123.txt in a folder (with permissions for just those users to write), script would read it every minute to get params, execute the task and then delete (or move) the file.