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.
Script to fetch currently logged in user details
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.
Script to log off idle users on Windows devices
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.
After executing the script, it will log off all the idle users on the Windows devices except currently active users.