Skip to content
Menu

Run a Command as the Logged-In User (From SYSTEM)

Runs your PowerShell code as the signed-in user, with that user's rights and registry, and returns up to 40 lines of output. High risk: needs DryRun=false and Approve=invoke-as-logged-in-user.

Run a command as the logged-in user (from SYSTEM) is free for members.

Enter your email to create your free account and get the file.

We'll email you the script and the weekly MSP Outpost email. Unsubscribe anytime. Privacy policy.

Already a member? Sign in.

Be the first to review Run a command as the logged-in user (from SYSTEM). Tell other MSPs how it worked for you.

Write a Review

LanguagePowerShell
Runs onWindows 10, Windows 11, Windows Server 2016+; runs as SYSTEM
NeedsPowerShell 5.1, administrator rights, a signed-in user
Changes thingsChanges what it runs on

What It Does

This script changes whatever your code changes, so it is a dry run until you set DryRun=false and Approve=invoke-as-logged-in-user. Before that it checks the user and reports what it would run. A run counts as one change. If nobody is signed in the result is SKIPPED and nothing runs.

It finds the user signed in at the console, or in an RDP session with an Explorer shell, runs your PowerShell code in that interactive session through a one-shot scheduled task, waits for it, and prints up to 40 nonblank output lines as rows, cutting each to 180 characters. The code runs with that user's rights only, sees that user's HKCU, profile and mapped drives, and has no visible window. A non-zero exit code or an error thrown by the code ends the run as ERROR with the first line of the output in the answer. Code that runs longer than TimeoutSeconds is stopped and the run ends as ERROR.

Give the code in Code, up to 50000 characters, or in CodeBase64 as UTF-8 Base64 for an RMM that refuses the characters PowerShell needs. Set one, not both. The code itself is never written to the change log, the result, the Json output or an error message, because snippets often hold paths, names or tokens. The log line holds only the user, the length of the code and the first 12 characters of its SHA-256. The code is written to a folder that only SYSTEM, Administrators and that user can read for the seconds it runs, then deleted, but do not put secrets in it all the same.

The output does show in the result rows and may reach your RMM's job history, so do not print secrets. The script cannot know what your code does: it can delete the user's files or change their settings.

Before You Run It

  • Test the snippet by hand first, as a normal user on a test PC. Nothing here checks what it does.
  • Run it as a dry run first to see which user would run it and that someone is signed in.
  • Use CodeBase64 if your RMM mangles $ ; | & > < or backticks. In PowerShell, [Convert]::ToBase64String([Text.Encoding]::UTF8.GetBytes('whoami')) makes it.
  • Keep secrets out of the code and out of its output. The output can reach your RMM's job history.
  • Tell the user if the code touches their files or settings.

Type and Risk

  • Type: Action. It makes its change, checks the change worked and says what changed. It exits 0, or 2 when it could not do the job.
  • Risk: High. It is a dry run unless you set both DryRun=false and Approve=invoke-as-logged-in-user, and it stops before making more than 25 changes in one run unless you raise MaxChanges. Run it once as a dry run and read the list first.
  • What it changes: Runs the supplied PowerShell code in the signed-in user's session; what it changes depends on that code.
  • Restart: It never restarts the machine.
  • Typical run: Seconds, up to TimeoutSeconds.
  • Example answer: "Ran code as CONTOSO\jsmith: exit 0, 3 output line(s)"
  • Tested before listing on Windows 11 with Windows PowerShell 5.1, run as SYSTEM, against its own test cases.

Inputs

Pass an input as an argument (-Name value), as an environment variable with the same name, which is how most RMMs pass script variables, or change its default in the param() block at the top of the script. An argument wins over an environment variable, which wins over the default.

  • Code: The PowerShell snippet to run, as text. Up to 50000 characters.
  • CodeBase64: The same snippet as UTF-8 Base64 text, for RMMs that cannot pass the characters PowerShell needs.
  • TimeoutSeconds: How long to let the code run before it is stopped. Whole number, 5 to 600. Default 120.

Every script also takes DryRun, OutputFormat (Text, Answer, Json or Csv), ExportPath and ResultField, which writes the answer to an RMM custom field.

License

Provided "as is" under the MIT License, without warranty of any kind. Test before production use. MSP Outpost and User First Digital Inc. accept no liability for any damage or loss.

The Script

invoke-as-logged-in-user.ps1 · 849 lines · 43.5 KB

Free for Members

Create a free account or sign in to read, copy and download this script.

Welcome Back

Sign in to your MSP Outpost account.