# 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](https://www.hexnode.com/mobile-device-management/help/executing-custom-scripts-for-windows/) remote action.

 Note: 
The script is supported on Windows 10 and 11 devices.

 

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

 

Script to fetch currently logged in user details
------------------------------------------------

Script to fetch currently logged in user details on Windows devices

quser 

   1

  quser 

   

 

  

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](https://cdn.hexnode.com/mobile-device-management/help/wp-content/uploads/2024/06/Script-to-fetch-user-details-on-Windows-devices.png "Script to fetch user details on Windows devices")](https://cdn.hexnode.com/mobile-device-management/help/wp-content/uploads/2024/06/Script-to-fetch-user-details-on-Windows-devices.png)

Script to log off idle users on Windows devices
-----------------------------------------------

Script to log off idle users on Windows devices

\# Get the current session ID $currentSessionId = (Get-WmiObject -Class Win32\_ComputerSystem).UserName -split "\\\\" | Select-Object -Last 1 # Get the list of sessions $sessions = quser # Loop through each session foreach ($session in $sessions) { $sessionInfo = $session -split "\\s+" $sessionId = $sessionInfo\[2\] $sessionState = $sessionInfo\[3\] # Check if the session is idle and not the current session if ($sessionState -eq "Disc" -and $sessionId -ne $currentSessionId) { # Log off the idle session logoff $sessionId } } 

   1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

  \# Get the current session ID 

$currentSessionId = (Get-WmiObject -Class Win32\_ComputerSystem).UserName -split "\\\\" | Select-Object -Last 1

\# Get the list of sessions 

$sessions = quser

\# Loop through each session 

foreach ($session in $sessions) {

 $sessionInfo = $session -split "\\s+"

 $sessionId = $sessionInfo\[2\]

 $sessionState = $sessionInfo\[3\]

\# Check if the session is idle and not the current session 

 if ($sessionState -eq "Disc" -and $sessionId -ne $currentSessionId) {

 \# Log off the idle session 

 logoff $sessionId

 }

} 

   

 

  

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](https://cdn.hexnode.com/mobile-device-management/help/wp-content/uploads/2024/06/Before-executing-the-script-to-log-off-idle-users.png "Before executing the script to log off idle users")](https://cdn.hexnode.com/mobile-device-management/help/wp-content/uploads/2024/06/Before-executing-the-script-to-log-off-idle-users.png)

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](https://cdn.hexnode.com/mobile-device-management/help/wp-content/uploads/2024/06/After-executing-the-script-to-log-off-idle-users.png "After executing the script to log off idle users")](https://cdn.hexnode.com/mobile-device-management/help/wp-content/uploads/2024/06/After-executing-the-script-to-log-off-idle-users.png)

 Notes:- 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.