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
(22)
29,831 times
Add to favorites
9/23/2015
E-mail Twitter del.icio.us Digg Facebook
Sign in to ask a question


  • Getting this script to read a .txt or .csv
    2 Posts | Last post Sun 2:58 AM
    • Great script, but for this to be any use to me I need it to have the ability to read a .txt or .csv file that has a list of my servers and then output to another .csv or .html. There was one solution below but it didn't work. when I use .\get-loggedonuser.ps1 .\serverlist.txt | export-csv .\getloggedonuser.csv -notypeinformation the error I get back is "A parameter can not be found that accepts argument 'l'. at line:1 char:1
    • Hello Robg77,
      
      You could run something like this:
      
      Get-Content -Path .\ServerList.txt | ForEach-Object {.\Get-LoggedOnUser.ps1 -ComputerName $_}
  • Run script referencing a file
    2 Posts | Last post Sun 2:56 AM
    • Hi there, First of all thanks for creating the script. I am new to PowerShell and learning little each day. This script really helps in my daily task. Can I run the script referencing a text file that contains all the PC's in the network?
    • Hello CGLiving,
      
      Yes, assuming you have a text file with a computer name on each line you could run something like this:
      
      Get-Content -Path .\Servers.txt | ForEach-Object {.\Get-LoggedOnUser.ps1 -ComputerName $_}
      
      Let me know how that works for you!
  • Calling from batch file
    2 Posts | Last post February 09, 2019
    • I am trying to call from batch file.
      
      powershell.exe -file ".\Get-LoggedOnUser.ps1" -ComputerName "a123,b456" >> log.txt 2>&1
      
      However, when I run with 2 or more computer names, i get the following error.
      
      Error 0x000006BA enumerating sessionnames
      
      Works fine when I only pass in one computer name.
      
      Please advise!
      
    • Alright, simple solution...
      
      use command not file...
      
      powershell.exe -command ".\Get-LoggedOnUser.ps1" -ComputerName a123,b456 >> log.txt 2>&1
      
      
  • Format Table output
    1 Posts | Last post January 15, 2019
    • 3o4
      is it possible to format this as a table instead of list?
  • Add 'Display Name' Corresponding to Username
    4 Posts | Last post December 14, 2018
    • Great script! I am running your script with export to CSV to produce a nicely formatted output report that is very useful, e.g.:
      .\Get-LoggedOnUser.ps1 | Export-Csv .\Get-LoggedOnUser.csv -NoTypeInformation
      
      Would it be possible to add an additional column next to UserName with the corresponding Display Name?
      I tried adding a new variable DisplayName under the $HashProps section to convert the username to display name using Get-ADUser, similar to this:
      DisplayName = (Get-ADUser -Identity $session.UserName -Properties DisplayName).DisplayName
      
      But haven't been able to get it to work with the existing current line structure sequence... is there a way to add Display Name into your existing script structure?
    • Hello!
      Try this:
      
      DisplayName = (Get-ADUser -Filter * -Properties DisplayName | Where-Object {$_.sAMAccountName -eq $CurrentLine[0]}).DisplayName
    • More details:
      
      <...>
      
      $HashProps = @{
          UserName = $CurrentLine[0]
          DisplayName = (Get-ADUser -Filter * -Properties DisplayName |
            Where-Object {$_.sAMAccountName -eq $CurrentLine[0]}).DisplayName
          ComputerName = $Computer
      }
      
      <...>
      
      Also, you need to add DisplayName property to 2 strings below:
      
      Select-Object -Property UserName,DisplayName,ComputerName,SessionName,Id,State,IdleTime,LogonTime,Error
      
    • perlestius, thank you very much!
      The DisplayName code that you suggested worked perfectly!
      Querying the Display Name properties from the $CurrentLine[0] object was the logic that I was missing. Thanks again for your very helpful response!
  • Get-LoggedOnUser from a non-domain joined computer
    1 Posts | Last post November 07, 2018
    • Hi Japp,
      
      Great code! 
      
      Can I run this script from a non-domain joined computer by passing my domain username/password. Similar to what I did below to get a user details with the surname of 'Taylor'.
      
      Import-Module ActiveDirectory
      $User = "remotedomain\myusername"
      $PWord = ConvertTo-SecureString -String "mypassword" -AsPlainText -Force
      $Credential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $User, $PWord
      Get-ADUser -Server remotedomain -Credential $Credential -Filter {Surname -like 'Taylor'}
      
      Can you please advise how I can pass my domain credentials and the remote domain into your script ?
      
      Many thanks,
      
      Mo
  • Cater for username containing a space
    2 Posts | Last post August 01, 2018
    • Hi Jaap,
      
      Great script. Have you updated the script of late to cater for a user name that contains a space.  The split obviously of array items is therefore out of sequence.
      
      Thanks AjN3806
    • Changing...
      $CurrentLine = $_.Trim() -Replace '\s+',' ' -Split '\s'
      ...to...
      $CurrentLine = $_.Trim() -Replace ' {2,}',',' -Split ','
      
      ...caters for a Username containing a single space, but assumes there is always at least 2 spaces between columns.
  • I am getting output on my mail tried so many times it shows script failed due to call depth overflow
    1 Posts | Last post June 11, 2018
    • I am not getting output on my mail tried so many times it shows script failed due to call depth overflow
  • Prompt for Computer name?
    1 Posts | Last post December 29, 2017
    • SO how do I set this to prompt for the computer name that I would want to search, rather than manually drop it into the script each time? I know I have to set it up to ask for a value for $ComputerName, but I'm not sure what to add to do that.
      
      Otherwise, this script is awesome. Thank you!
  • 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{
          [CmdletBinding()]
          param
          (   [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
      SteveD
      
    • adapted from David Knight Sep 21, 2016 Answer - found here
      https://superuser.com/questions/587737/powershell-get-a-value-from-query-user-and-not-the-headers-etc
      
      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
      https://rcmtech.wordpress.com/2014/04/08/powershell-finding-user-sessions-on-rdsh-servers/
1 - 10 of 29 Items