title: Logoff Disconnected user sessions based on IDLE time
body: <p>I have written a PS script to find any user who has a disconnected RDP session on remote servers and I got help from user @Theo to get it finished.</p>
  <p>Now add on to my script, I want to Log off the disconnected users if the IDLE time is more than 1 day.</p>
  <p>This will help us a lot for me to achieve the things</p>
  <p><strong>CODE</strong></p>
  <pre><code>## Clear Host Console
  Clear-Host
  
  ## Define Variable for Server Count
  $z = 0
  
  ##Set Default Script Location
  Set-Location $PSScriptRoot
  
  ## Provide List of Servers to Check for the Disconnected user session
  $Servers = Get-Content &quot;.\Servers\DA_Servers.txt&quot;
  
  ## Get Servers Count
  $count = $Servers.count 
  
  ## Define Date for the Out file
  $dt = Get-Date -Format yyyyMMdd
  $Date = Get-Date
  
  ## Define Path for the Out File
  $exportFile = &quot;.\Out\RDP_DisConnected_Users.csv&quot;
  
  ## Create a Function to list all user sessions from Remote Servers
  function Get-RemoteUsers {
  
  ## Loop through each server to find the User session    
  foreach ($Computer in $Servers) {
  
  #initiate counter for showing progress
  $z = $z + 1
  
  # Start writing progress 
  Write-Progress -Activity &quot;Processing Server: $z out of $count servers.&quot; -Status &quot; Progress&quot; -PercentComplete ($z/$Servers.count*100)
  
  $obj = &quot;&quot; | Select-Object @{Name = 'ServerName'; Expression = {$Computer}}, UserName, ID, State, IdleTime, LogonTime
      
    try {
           quser /server:$Computer 2&gt;&amp;1 | Select-Object -Skip 1 | ForEach-Object {
           $items = $_.Trim() -split '\s{2,}'
           $obj.UserName     = $items[0]
                   
  # If session is disconnected different fields will be selected
      if ($items[2] -like 'Disc*') 
      {
           $obj.Id          = $items[1]
           $obj.State       = $items[2]
           $obj.IdleTime    = $items[3]
           $obj.LogonTime   = $items[4..($items.GetUpperBound(0))] -join ' ' 
       }
      else {
            $obj.Id          = $items[1]
            $obj.State       = $items[2]
            $obj.IdleTime    = $items[3]
            $obj.LogonTime   = $items[4] 
           }
  
  # reformat the IdleTime property
            $obj.IdleTime = '{0} days, {1} hours, {2} minutes' -f ([int[]]([regex]'^(?:(\d+)\+)?(\d+):(\d+)').Match($obj.IdleTime).Groups[1..3].Value | ForEach-Object { $_ })
          # output the object
            $obj
        }
     } 
    catch {
              #$obj.Error = $_.Exception.Message
              #$obj
          }
      }
  }
  
  ## Filter the results to find out the disconnected users
  $allRemoteUsers = Get-RemoteUsers  $Servers
  $disconnectedUsers = $allRemoteUsers | Where-Object {$_.State -like 'disc*' }
  if (@($disconnectedUsers).Count) {
      #output on screen
      $disconnectedUsers | Format-Table -AutoSize
      # output to Csv
      $disconnectedUsers | Export-Csv &quot;$exportFile&quot; -NoTypeInformation
  }
  else {
      Write-Host &quot;No disconnected users found&quot; -BackgroundColor Red
  }
  </code></pre>