Category filter

Script to log off idle users on Windows devices

This document helps you to log off idle users on Windows devices with the help of a script. Idle user sessions on Windows devices occur when individuals log in but do not interact with the system for a specific period. The situation also may arise when users leave their sessions unattended or lock their screens without logging off. Idle user sessions can still consume system resources like memory and CPU cycles. Idle user sessions can also lead to slower performance of the device for active users, and it may also drain the battery. Organizations with shared Windows devices can use a script to log off idle users on Windows devices. IT administrators can execute the script to log off idle users on Windows devices with the help of Hexnode’s Execute Custom Script remote action.


The script is supported on Windows 10 and 11 devices.


The sample script provided below are adapted from third-party open source sites.

Script to fetch currently logged in user details

The above script retrieves details of all logged-in users on Windows devices. It fetches information such as username, session ID, session state (active or disconnected), idle time (in minutes), and logon time for each user on the Windows device.

Details of the users including idle users

Script to log off idle users on Windows devices

The above PowerShell script retrieves all user sessions that are currently initiated on the device. The script utilizes the ‘quser’ command to gather information about the user sessions. For each session, the script checks if any user is idle except for the current active user. If any idle or disconnected user are found, the script logs them off, effectively terminating idle sessions.

Idle users on the device

After executing the script, it will log off all the idle users on the Windows devices except currently active users.

Run a script to successfully identify and log off idle users


  • It is recommended to manually validate the script execution on a system before executing the action in bulk.
  • Hexnode will not be responsible for any damage/loss to the system on the behavior of the script.

  • Sample Script Repository