Force log off the active interactive user on Windows endpoints to clear sessions for patching or incident response
This Automox Worklet™ force logs off the active interactive user on a Windows endpoint and confirms the session has fully terminated before exiting. The evaluation phase reads the Win32_ComputerSystem UserName property through Get-CimInstance to detect whether anyone is signed in. If a session is present, remediation invokes the Win32Shutdown method on Win32_OperatingSystem with the forced-logoff flag (value 4), which is the WMI equivalent of shutdown.exe /l /f.
The Worklet does not depend on logoff.exe, quser, or query session being available, which matters on Windows Server Core and on workstations where Remote Desktop Services tooling is not installed. It works the same on Windows 10, Windows 11, Windows Server 2016, 2019, and 2022, because Win32_OperatingSystem ships with every supported Windows SKU.
After issuing the logoff, the script enters a verification loop. It re-queries Win32_ComputerSystem every five seconds for up to three minutes and exits 0 only when UserName is null. If the session is still alive at the timeout, the Worklet writes a failure message to the Automox Activity Log and exits non-zero, so unresponsive endpoints surface in policy reporting instead of going silent.
An active interactive session is one of the most common blockers for end-to-end Windows patching. Cumulative updates that touch the servicing stack, .NET runtime upgrades, and CrowdStrike or SentinelOne agent rollovers all need the user signed off to release file handles in C:\Windows\System32 and C:\ProgramData. A single stuck RDP or console session can keep a CVE patch in a half-applied state for days, and the Activity Log on a fleet of one thousand endpoints will not tell you which ones unless something forces the session closed and reports back.
The same logoff path is also the fastest contain-now action during an incident. Security operations needs to kick a session off the moment a credential is suspected compromised, before the analyst on call can decide whether to disable the AD account or wipe the endpoint. Running this Worklet against an endpoint or a group of endpoints terminates Explorer, all user-mode processes, and any open RDP channel within seconds, which is the WMI-driven equivalent of opening Task Manager on the box and signing the user out.
Patch rollups, incident-response cleanup, and forced password resets all require active sessions to terminate first, and walking thousands of help-desk tickets through Ctrl+Alt+Del is not a strategy. This Worklet invokes the Win32Shutdown forced-logoff method under the Automox SYSTEM context on every Windows endpoint in scope, returns the result through the Activity Log, and pairs cleanly in front of patch or remediation policies that need an idle user session to land safely.
Evaluation phase: The Worklet runs Get-CimInstance -ClassName Win32_ComputerSystem and reads the UserName property. UserName populates only when an interactive console session is active, so a null value means the endpoint is idle and the policy exits 0 without remediation. A populated value flags the endpoint as non-compliant against the policy and queues remediation. The evaluation does not touch Win32_LoggedOnUser, which would also include service logons and RDP listeners and would produce false positives on a server.
Remediation phase: The script pipes Get-CimInstance -ClassName Win32_OperatingSystem into Invoke-CimMethod -MethodName Win32Shutdown -Arguments @{ Flags = 4 }, where flag 4 is Forced Logoff. This is the CIM-level equivalent of shutdown.exe /l /f and does not depend on logoff.exe, query session, or quser being present on the SKU. It then enters a do-while loop that sleeps five seconds between checks and runs for up to 180 seconds of elapsed time, re-querying Win32_ComputerSystem UserName each cycle. The loop returns with exit code 0 the moment UserName becomes null, or writes a timeout failure to the Automox Activity Log and exits 1 if a session is still attached after three minutes.
Windows 10, Windows 11, or Windows Server 2016 and later, on any SKU including Server Core (the Worklet does not depend on logoff.exe, quser, or query session)
PowerShell 3.0 or later, which ships in-box on every supported Windows release (the script header lists PowerShell v3+ as the prerequisite)
The Automox agent's default SYSTEM context, which already holds the SeShutdownPrivilege required to invoke Win32Shutdown
FixNow-compatible: trigger on demand from the Automox console for incident response, or schedule on a policy for recurring maintenance enforcement
Unsaved work in the active session is discarded. Pair the Worklet with a user-facing toast notification Worklet five to fifteen minutes earlier when the use case allows a grace period
Multi-session hosts (Remote Desktop Session Host, AVD personal pools) are out of scope. Win32_ComputerSystem reports only the console user; use a dedicated RDS Worklet that walks Win32_LoggedOnUser for those hosts
Within seconds of remediation, the user-mode session tears down. Explorer.exe and all child processes under the user's SID exit, open documents close without prompting to save, mapped drives detach, and the endpoint returns to the lock screen showing Other user or the configured default tile. A subsequent Get-CimInstance -ClassName Win32_ComputerSystem on the endpoint returns an empty UserName property, and quser.exe (where available) lists no sessions in the Active state. The Worklet exits 0 and the Automox Activity Log records the username that was signed out and the timestamp the loop confirmed the session was clear.
If the verification loop reaches the three-minute timeout with a session still attached, the Worklet exits non-zero and writes the offending UserName to the Activity Log so the on-call admin can investigate. The most common causes are a blocking driver dialog, a hung legacy application that refuses to terminate within the user-mode timeout, or an endpoint that has lost responsiveness on the WMI provider host. Escalate those cases to a hard reboot Worklet or to a remote support session. Once any pending patches install, the next evaluation pass against the endpoint will find UserName null and report compliance without taking further action.


Loading...
Consider Worklets your easy button
A Worklet is an automation script, written in Bash or PowerShell, designed for seamless execution on endpoints – at scale – within the Automox platform. Worklets deploy named-CVE mitigations within hours of disclosure, perform configuration, remediation, and install or remove applications and settings across Windows, macOS, and Linux.

AUTOMOX + WORKLETS™
Uncover new possibilities with simple, powerful automation.
By submitting this form you agree to our Master Services Agreement and Privacy Policy
By submitting this form you agree to our Master Services Agreement and Privacy Policy.
Already have an account? Log in