r/PowerShell • u/leuit • May 22 '26
Script Sharing I wanted to run scripts in the logged-on user’s security context, so I built a PowerShell module
I kept running into situations where a fix needed to happen in a logged-on user session (HKCU, Explorer, user-installed apps, notifications, browser settings, Outlook auth, etc.), but most automation tools execute as NT AUTHORITY\SYSTEM or require the user’s password.
Existing approaches definitely exist, and some are quite clever, but I kept running into tradeoffs that didn’t fit what I wanted:
- Asking for the user’s password
- Interrupting the user session
- Scheduled task workarounds
- Poor targeting in terminal server / multi-session environments
- Remoting-style object serialization limitations when passing data back and forth
So I built PSUserContext: a binary PowerShell module for running scripts in another user's interactive session and security context.
Some scenarios where this has been useful:
- Restarting user processes (
explorer.exe, Teams, browsers) - User profile remediation
- Outlook profile/config fixes
- User-scoped app troubleshooting
A few design goals:
- Execute from SYSTEM / RMM contexts
- Run inside an existing logged-on user session
- No user password required
- No session takeover or interruption
- Better object handling than traditional remoting serialization
GitHub:
https://github.com/walliba/PSUserContext
Still actively evolving, so I’d appreciate feedback, criticism, weird edge cases, or “why didn’t you just do X?” from people who’ve solved similar problems.
13
u/thomsxD May 22 '26
Pretty sure PSADT has this.
2
u/vermyx May 22 '26
End point security process will usually flag and stop this behavior. There's a reason why local agents running under the user context is usually preferred.
2
u/joe210565 May 23 '26
almost all RMM solutions can run as system and user context, i do scripts in my company so I also run them on specific context depending on need.
2
u/Over_Dingo Aug 11 '26
How would you execute it on a remote machine? Install it on it and run via PSExec / PSRemoting ?
5
u/kevinelwell May 22 '26
When I used to do software packaging/automation, we leveraged “Active Setup”.
1
u/KevMar Community Blogger May 22 '26
So are you doing Win32 API calls or WMI to trigger that (or something else)? Does the user have to be logged in or can you target specific users?
Making it a binary module might make it harder to get approved to run in some environments but it would make it easier to add a specific security policy to allow it.
1
u/realslacker May 23 '26
Check out Invoke-CommandAs
1
u/krzydoug May 23 '26
Very nice. NinjaOne and other RMMs offer an interactive session. Any chance you could add that capability to your module? I'd love to make a "open powershell as user" in screenconnect backstage
1
u/EuphoricFly5489 May 24 '26
Dropping this gem which works for me https://github.com/KelvinTegelaar/RunAsUser
0
u/CyberChevalier May 22 '26
Legally being able to act as the user can have huge impact…
I made a script based on scheduled task running as « logged on user » but my legal team told me it was not a good idea as I can then impersonate the user at every levels…
1
u/PanosGreg May 23 '26 edited May 23 '26
I use something very similar to the following, in production (primarily on SQL Server clusters).
The context is that the code is running under the local SYSTEM account (this is the AWS SSM Agent service which runs as system), and then I run a specific scriptblock as a separate AD account which has access to SQL Server (that has permissions to Failover AGs, pause/resume DBs, etc)
All credits go to Jordan Borean for his work on the
Invoke-WithImpersonationfunction, I just made a few changes to expand its functionality.You can have a look at the examples in the function on how it can be used.
Invoke-WithImpersonation.ps1
Edit: a) you have to run this from SYSTEM, a regular local Admin account won't do, cause it needs a specific Privilege that SYSTEM has by default. b) the user that you are impersonating does not need to be logged in on the machine. c) this function does not spawn a new process. d) you do need to provide the user/pass for the account you want to impersonate
1
u/Deep_Ad1959 May 28 '26
the part that gets people on this is session 0 isolation. anything running as SYSTEM lives in session 0 with no desktop, so spawning a process won't show up for the interactive user. WTSQueryUserToken into DuplicateTokenEx into CreateProcessAsUser is the canonical path, but WTSQueryUserToken needs SE_TCB_NAME, which only SYSTEM has, so the module only works from a service or task running as SYSTEM, not a regular admin shell. the other gotcha is HKCU, the hive isn't mounted unless you LoadUserProfile first, otherwise the script silently writes to .DEFAULT and the user never sees the change. worth calling out in the readme which of those cases your module handles. written with ai
1
u/Deep_Ad1959 May 28 '26
the security-context part isn't the interesting problem here, scheduled tasks under SYSTEM impersonating a user have worked for a decade. the part nobody solves is keeping the consumers of your module aware that you shipped v0.4 with a new flag. github watch is functionally dead, the gallery surfaces nothing, and a changelog in the readme gets read once on install and never again. shipping is half the work, the other half is a heartbeat your users can subscribe to without remembering to check.
18
u/[deleted] May 22 '26 edited May 22 '26
[removed] — view removed comment