How can an elevated PowerShell script launch another script without admin rights?

0
3
Asked By MellowPine42 On

I have a PowerShell script that runs with elevated administrator privileges. At the end, it needs to start a second script in the currently logged-on user's normal, non-elevated context. I've tried starting a new process and invoking PowerShell, but the child process keeps inheriting the administrator token. What's the best way to launch the second script without elevation?

4 Answers

Answered By CobaltVessel18 On

It’s usually easier to reverse the flow: start the main script in the normal user context, have it launch the administrative portion with elevation, wait for that to finish, and then continue with the non-elevated steps. That way the final process doesn’t inherit an administrator token.

MellowPine42 -

The first script is triggered automatically when a user is terminated. I’ll test this approach and see whether it fits that workflow.

Answered By SilverMaple53 On

For a script that must begin elevated, you can launch a new process using the logged-on user’s Explorer process as its parent. Explorer normally runs unelevated, so the child process receives the user’s standard token instead of inheriting the administrator token. A PowerShell process-management module such as ProcessEx can use Explorer’s startup information for this purpose. Find the Explorer instance with the same session ID as the logged-on user, then use it as the parent when starting the second PowerShell script.

Answered By QuietHarbor7 On

A scheduled task can handle this, especially if you configure it to run under the target user account without elevated privileges. It requires a little setup, but it’s a reliable option for scripts that run during user-management workflows.

Answered By AmberQuill26 On

Another option is to use a user-context trigger. Have the administrative script signal an event, and have a process already running in the user session listen for that event and launch the non-elevated script. This avoids trying to downgrade an already elevated process directly.

Related Questions

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.