Hello @Baharul Islam ,
Thanks for your query and apologies for the delay.
Can you try out the below script and try to test in the machine where multiple multiple users were logged-in.
Below script uses the output of quser command
C:\Users\user>quser
USERNAME SESSIONNAME ID STATE IDLE TIME LOGON TIME
user1 rdp-tcp#25 7 Active . 3/1/2021 11:28 PM
user2 rdp-tcp#15 11 Active 5 3/2/2021 12:53 AM
In the above output user2 was idle for 5 mins and active users are indicated by IdleTime = .
Below script will query all the users , validate the users IdleTime.
If the IdleTime >= 1 , it will invoke logoff user using the corresponding session ID
You can schedule this script in the task manager of all the machines
Execute below powershell module on the machine where you want to kill idle users who are in-active for more than 1 min
function Query-UsersInfo
{
[CmdletBinding()]
Param (
[Parameter ()]
[string[]]
$ComputerName = $env:COMPUTERNAME
)
begin
{
$Header = 'UserName','SessionName','ID','State','IdleTime','LogonTime'
$NotAbleToConnect = 'NotAbleToConnect'
}
process
{
foreach ($cn in $ComputerName)
{
if (Test-Connection -ComputerName $cn -Count 1 -Quiet)
{
quser /server:$cn | #QUery all the user sessions
Select-Object -Skip 1 | #Skip the header
ForEach-Object {($_ -replace '\s{2,}', ',').Trim()} |
ConvertFrom-Csv -Header $Header |
ForEach-Object {
#loop through each user
$IdleTime = $_.IdleTime #Get Idle time
if ($IdleTime -ne ".")
{
#Create a customer Powershell object to query
$obj = [PSCustomObject]@{
ComputerName = $CN_Item
UserName = $_.UserName
SessionName = $_.SessionName
ID = $_.ID
State = $_.State
IdleTime = $IdleTime
LogonTime = [datetime]$_.LogonTime
}
#Check for how long user is idle
#Here I am checking for 1 min
if ([int]$obj.IdleTime -gt 1) #(i.e. If IdleTime > 0)
{
logoff $obj.ID #Logoff the user
}
}
}
}
}
}
}
Query-UsersInfo "localhost"
Let us know if you need additional help .
If the above script helps , kindly "Upvote and Accept the Answer"