Get-LoggedOnUser Gathers information of logged on users on remote systems

This script utilizes quser to query either the local or remote system and parses this into PowerShell objects using the ConvertFrom-Csv Cmdlet. It supports pipeline input or an array of computers in the ComputerName parameter.

4.9 Star
34,253 times
Add to favorites
E-mail Twitter Digg Facebook
  • query remote server, if certain user logs off, send email
    4 Posts | Last post November 29, 2017
    • Nice Job, very pretty!
      Could I use this script to query a remote server, if a particular user logs off, then send email. This would be scheduled task on another server to check the remote server every few minutes.
      I have some sloppy code wrapped around "quser" using regex to pull the username out, and message if it's not logged in, but after reading all the Q-n-A, this script looks way more versatile for other sysadmin jobs.
    • Actually the answer was actually in your code:
              $quserOut = quser /SERVER:$computer 2>&1
              if ($quserOut -match "No user exists")
              $Subject = "$Computer ATTENTION NEEDED $User is NOT logged in"
              $body = "Server: $Computer, ATTENTION maybe needed for $Computer it does not appear to be running"
              EmailIT $Subject $Body ; continue }
              $users = $quserOut -replace '\s{2,}', ',' | ConvertFrom-CSV -Header 'UserName' # | Add-Member -MemberType NoteProperty -Name ComputerName -Value $computer -PassThru
      if ($users[1] -match $User) { ...
      Thanks -- I have booked marked this page for all the great examples to use your original scirpt. For now though, this change in code works perfectly!
    • I got stuck verifying if the users is not logged on error.Here is the final edited script:
      IsUserLoggedin -- (without emailIT function) 
      function Get-LoggedInUser{
          (   [ValidateNotNullOrEmpty()]
              [String[]]$ComputerName = $env:COMPUTERNAME)
          $out = @()
          ForEach ($computer in $ComputerName)
          {   try { if (-not (Test-Connection -ComputerName $computer -Quiet -Count 2 -ErrorAction Stop)) {
      $Subject = "PING RESULTS: ATTENTION $computer did not pass PING test "
      $Body = "PING RESULTS: ATTENTION $computer did not pass PING test, Please double check if the VM is running and connected to the network"
      EmailIT $Subject $Body }  }
      catch {$Subject = "CATCH Ping Results: Can't test connect to $computer"
      $Body = "CATCH from Ping test failed,Ping Results: Can't test connect to $computer, check to make sure VM is running and Conneted to the network"
      EmailIT $Subject $Body }
      $User="Any local or AD user"
      $ErrorActionPreference = "Continue"
      $quserOut = quser.exe /SERVER:$computer 2>&1
      $ErrorActionPreference = "Stop"
      if ($quserOut -match "No User exists for")  {
      $Subject = "$Computer ATTENTION NEEDED $User is NOT logged in"
      $body = "Server: $Computer, ATTENTION maybe needed for $Computer it appears as though $User is not logged in or $Computer is not running"
      EmailIT $Subject $Body}
      $users = $quserOut -replace '\s{2,}', ',' | ConvertFrom-CSV -Header 'UserName' }
      if ($users[1] -match $User ) { 
      $Subject ="$Computer Login $User Check PASSED " 
      $body = "Server: $Computer is on and $User is currently Logged in"
      EmailIT $Subject $Body
      } Else
      {$Subject = "$Computer ATTENTION NEEDED $User is NOT logged in"
      $body = "Server: $Computer, ATTENTION maybe needed for $Computer does not appear to have $User logged in or $Computer may not be running"
      EmailIT $Subject $Body}  
      Enjoy :) Let me know if you ever use the code
    • adapted from David Knight Sep 21, 2016 Answer - found here
      But like I said Jaap Brasser had the answer in his code!
      Also to get rid of error from "No User Exists for"
      $ErrorActionPreference = "Continue" --- author Robin CM's IT Blog
  • Display "ClientName"
    3 Posts | Last post October 11, 2017
    • On the Sessions tab in Remote Desktop Services Manager there is a "ClientName" column. Is there a way to capture that as well?
    • Hello Patrick,
      Yes this is possible, I have opened up a GitHub issue so you can track my work on this request, it is available here:
      Jaap Brasser
    • Add Servername line by line to ServerList.txt :
      $computername = Get-Content  C:\Scripts\Loggedonuser\ServerList.txt
      View On Screen :
      .\Get-LoggedOnUser –computername $computername | ft -AutoSize
      View as CSV file : 
      .\Get-LoggedOnUser –computername $computername | export-csv C:\Scripts\Loggedonuser\LoggedonUuers.csv -NoTypeInformation
      View as HTML file :
      .\Get-LoggedOnUser –computername $computername| ConvertTo-HTML | Out-File C:\Scripts\Loggedonuser\LoggedonUuers.html
  • how will i run it to multiple machines?
    1 Posts | Last post February 25, 2017
    • Im new to powershell.
      How do run it so I can get the result for all the machines and their computer name as well? 
  • How to send it as Mail?
    2 Posts | Last post February 14, 2017
    • Hello Jaap,
      THX for the Script, is there a way to send it as E-Mail?
    • Hello Mike,
      Absolutely, you could use the Send-MailMessage cmdlet for this purpose, for example:
      $EmailSplat = @{
          To          = ''
          From        = ''
          SmtpServer  = ''
          Subject     = 'Logged on users Reports from server X'
          Body        = .\Get-LoggedOnUser.ps1 | Out-String
      Send-MailMessage @EmailSplat
      Let me know how that works for you!
      Jaap Brasser
  • idletime=none
    2 Posts | Last post February 14, 2017
    • Hi Jaap,
      some computer returns "none" for idletime.
      What is the meaning of this ? 
    • Hello Onimamoglu,
      This means the session is currently active, if this is the case it can be listed as none or as ., depending on the operating system.
      Jaap Brasser
  • Displaying idle time to user?
    2 Posts | Last post November 03, 2016
    • Found this article while looking for something a bit different but hoping you have an answer or suggestion for me. In my business we have like 20 admins and logging on to 200+ servers, you 1) don't know if you just happened to disconnect someone else 2) cannot tell how long stuff that is open on that server has been idle. I was hoping to find a way to display how long the session was idle before I connect to be able to tell if someone is working or if that stuff was just forgotten open on the server 2w ago. 
    • Hello Borgsquirrel,
      This script displays both the logon time and the idle time, would that not provide you with enough information? Here is an example of the output of this script:
      UserName     : jbrasser
      ComputerName : localhost
      SessionName  : rdp-tcp#0
      Id           : 2
      State        : Active
      IdleTime     : .
      LogonTime    : 11/3/2016 9:37 AM
      Error        :
      UserName     : otheradmin
      ComputerName : localhost
      SessionName  : rdp-tcp#2
      Id           : 3
      State        : Active
      IdleTime     : 14:47
      LogonTime    : 9/27/2016 11:41 AM
      Error        :
      So this displays how long a user has been idle, unfortunately this does not display how long the processes that the user are running have been idle. This is something that does not have a clear answer either. If it is something simple, like a file transfer you can simply track disk activity for processes by that user.
      In general I would advise on using PowerShell Remoting capabilities for server management where possible, then RDP logons are not required and you won't have to disconnect your colleagues.
      Jaap Brasser
  • html
    4 Posts | Last post September 01, 2016
    • Hi Jaap,
      Is it possible to send the result of this script as an html file with e-mail
    • Absolutely, you could chose to either attach it to the email or send it as the body of an html email, the following example shows how to insert it into the body of an email:
      Send-MailMessage -From -To -Subject 'Your loggedon users HTML report' -SmtpServer -BodyAsHtml -Body (.\Get-LoggedOnUser.ps1 | ConvertTo-Html)
    • Hi Jaap,
      Thanks for the reply,
      I did add the line you send to the end of Get-LoggedOnUser.ps1 but get the error:
      Unexpected token 'Send-MailMessage' in expression or statement.
      I did also try to run the it from shell and get en error.
      Any idea?
    • Which version of PowerShell are you running, can you show the output of the following code:
      Also can you copy paste the entire line of code you are executing and the complete error message, that way I'll be able to examine what's causing the issue.
      Jaap Brasser
  • Output as CSV and email to admin
    2 Posts | Last post July 13, 2016
    • Hi Jaap,
      Could you please let me know how to output the data to a CSV file and get it emailed to admin?
      Yagya Shree
    • Sure, you could output it to csv using the following command:
      .\Get-LoggedOnUser.ps1 | Export-Csv 'C:\Script\YourFile.Csv'
      If you would like to email this you could do something along these lines:
      Send-MailMessage -Attachments C:\Script\YourFile.csv -From -To -Subject 'Your CSV File' -SmtpServer
  • Additional Functionality sought
    1 Posts | Last post January 07, 2016
    • Possible to have this script loop continuously until there are 0 users on the system and then reboot. I was thinking this could be a great tool for additional automation and this being the trigger for that to occur.
  • Question about this script
    2 Posts | Last post January 07, 2016
    • Get-Content ComputerList.txt | ForEach-Object { 
          .\Get-LoggedOnUser.ps1 -ComputerName $_
      } | Where-Object {@('Active','Disconnected') -contains $_.State} |
      Select-Object -ExpandProperty ComputerName
      If I want to do the above code, do I put that into another powershell script to run?
    • You could just paste this into the PowerShell console and execute it right away. If you expect to execute this more than once you could also store this as a .ps1 file with this code as its content.
11 - 20 of 30 Items