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


  • Query specific users
    2 Posts | Last post November 16, 2015
    • Hi, I would like to use the script to query users who have active sessions but are idle for longer then one day, so i can logoff those sessions. How would I do that?
      
      
    • Hello J. Miezenbeek,
      
      Yes this is possible when this script is used in combination with the Disconnect-LoggedOnUser script, here is the syntax that you require:
      
      .\Get-LoggedOnUser.ps1 | Where-Object {[datetime]$_.LogonTime -le (Get-Date).AddDays(-1)} | Disconnect-LoggedOnUser -Verbose
      
      My Disconnect-LoggedOnUser script is available here: https://gallery.technet.microsoft.com/scriptcenter/Disconnect-LoggedOnUser-116ea40c
      
      
  • more servers....
    4 Posts | Last post November 03, 2015
    • Hello Jaap
      indeed a very nice script, but I have two problems /questions about it;
      We have over 500 servers, and then it is not very convenient to process all servers manually into the script as the example that has been given.
      
      Second if the firewall is not annoying file and printer sharing the script gives a error for the specific server in question "Error 1722: The RPC server is unavailable", but when you put a lot of server into the script, who don’t know exactly which servers is giving this error.
      Is it possible that the script;
      A > Can look to one or more OU locations within Active directory?
      B > To have so sort of logging , which servers is successfully been processed and which ones give an error?
      That would really help me….
      Greetings,
      
      Charles
      
    • A: Couldn't you use Get-ADComputer -SearchBase 'OU=your,DC=jaap,DC=com' to gather the computeraccounts and then pipe that into this script using ForEach-Object? Alternatively you could also use the [adsisearcher] type accelerator for this purpose, then you don't require the ActiveDirectory module. Let me know if you need any help constructing the syntax for using either one of these solutions.
      
      B: I enabled error logging in two different ways, I added in a property that contains the error message if the tool cannot connect to a remote system. And the error message is also written to the $Error variable. To do this I set the $ErrorActionPreference for this script to stop and inserted a try-catch block.
      
      
      Let me know if this helps you with your questions.
    • > A: I can confirm that it's possible to use the Get-LoggedOnUser AND gather computeraccounts within different OUs with the syntax Jaap described above: Get-ADComputer -SearchBase 'OU=your,DC=jaap,DC=com' and a ForEach loop.
      
      Great Work!
    • Thanks for the feedback M_Petersen, I appreciate that you took the time to treat my code.
  • Fails with StrictMode on
    2 Posts | Last post September 23, 2015
    • With StrictMode=latest an exception may be triggered due to exceeding the array boundary when getting the value of Logon time (which is the last element, but it may be composed of 1, 2 or more delimited values due to local time format settings). 
      
      I had to change these lines to make it work:
      
      $HashProps.LogonTime = $CurrentLine[4..($CurrentLine.GetUpperBound(0))] -join ' '
      ...
      $HashProps.LogonTime = $CurrentLine[5..($CurrentLine.GetUpperBound(0))] -join ' '
      
      Hope this helps.
    • Cool that does make sense, I have taken your feedback and added it to the script. Thanks for taking the time to look at my script and suggesting this workaround. Personally I would have probably gone for the .Length property and substract 1 from the result, but GetUpperBound is a cleaner alternative.
      
      Thanks for your feedback!
  • Logoff all disconnected users
    4 Posts | Last post June 03, 2015
    • Would there be away to logoff disconnected sessions that are found
    • That is a good suggestion, I will create a script for that and post here once it is available. You can expect it soon.
    • Thanks looking forward to the update
    • Hello Condrovic, I have created an additional function named Disconnect-LoggedOnUser and uploaded it to the TechNet Script Gallery. It is available here: 
      https://gallery.technet.microsoft.com/scriptcenter/Disconnect-LoggedOnUser-116ea40c
      
      Or you can find it in the description of this script.
  • Very Nice!
    2 Posts | Last post January 30, 2015
    • I just wanted to comment and say that your script is very nice!  With the output being an object, I can then pipe that on through to log off the users with disconnected sessions!
      
    • Indeed, I'm glad you enjoy the script
  • Logged on User and the Status (State) into csv file
    2 Posts | Last post January 30, 2015
    • I have a requirement, Need to get all logged in users and their status (Active or disconnected)in to a CSV file... for set of servers 
      
      Much Appreciated if you provide me some guidelines or the script.. 
      
      Thanks in advance
      G
    • Assuming you are running the code from a computer that has the AD module available and PowerShell v3 you could run the following code:
      
      Import-Module ActiveDirectory 
      Get-ADComputer | 
      ForEach-Object { 
          .\Get-LoggedOnUser.ps1 -ComputerName $_.Name | 
          Export-Csv -Path LoggedOnUsers.csv -Append -NoTypeInformation
      }
  • One user over many servers
    4 Posts | Last post January 30, 2015
    • Hi!
      How can I modify this script to find a specific user over many servers?
      
      I had executed this:
      
      Import-Module ActiveDirectory 
      
      $Servers = Get-ADComputer -Filter {OperatingSystem -like "*server*"}
      
      ForEach ($Server in $Servers) { 
          $ServerName = $Server.Name 
      
          Write-Host "Consulting $ServerName" 
          
          D:\PowerShell\Get-LoggedOnUser.ps1 -ComputerName $Server.Name
      
      } 
      
      
      What you think?
    • Your code would definitely work for this purpose, I can suggest some minor improvements. The two main suggestions I would make are:
       - You set the $ServerName variable which isn't necessary as you can use expanding strings
       - You use ForEach instead of ForEach-Object in combination with the pipeline, using this allows for your script to start processing users before the Get-ADComputer cmdlet has fully processed providing slightly faster results.
      
      Furthermore I have added the Where-Object cmdlet, you can change -eq statement to only retrieve the results for a specific user.
      
      Here is the code:
      
      Import-Module ActiveDirectory 
      Get-ADComputer -Filter {OperatingSystem -like "*server*"} | 
      ForEach-Object { 
          Write-Host "Consulting $($_.ServerName)" 
          D:\PowerShell\Get-LoggedOnUser.ps1 -ComputerName $_.Name | 
          Where-Object {$_.UserName -eq 'domain\username'} 
      }
    • Your code would definitely work for this purpose, I can suggest some minor improvements. The two main suggestions I would make are:
       - You set the $ServerName variable which isn't necessary as you can use expanding strings
       - You use ForEach instead of ForEach-Object in combination with the pipeline, using this allows for your script to start processing users before the Get-ADComputer cmdlet has fully processed providing slightly faster results.
      
      Furthermore I have added the Where-Object cmdlet, you can change -eq statement to only retrieve the results for a specific user.
      
      Here is the code:
      
      Import-Module ActiveDirectory 
      Get-ADComputer -Filter {OperatingSystem -like "*server*"} | 
      ForEach-Object { 
          Write-Host "Consulting $($_.ServerName)" 
          D:\PowerShell\Get-LoggedOnUser.ps1 -ComputerName $_.Name | 
          Where-Object {$_.UserName -eq 'domain\username'} 
      }
    • Your code would definitely work for this purpose, I can suggest some minor improvements. The two main suggestions I would make are:
       - You set the $ServerName variable which isn't necessary as you can use expanding strings
       - You use ForEach instead of ForEach-Object in combination with the pipeline, using this allows for your script to start processing users before the Get-ADComputer cmdlet has fully processed providing slightly faster results.
      
      Furthermore I have added the Where-Object cmdlet, you can change -eq statement to only retrieve the results for a specific user.
      
      Here is the code:
      
      Import-Module ActiveDirectory 
      Get-ADComputer -Filter {OperatingSystem -like "*server*"} | 
      ForEach-Object { 
          Write-Host "Consulting $($_.ServerName)" 
          D:\PowerShell\Get-LoggedOnUser.ps1 -ComputerName $_.Name | 
          Where-Object {$_.UserName -eq 'domain\username'} 
      }
  • Output a timespan instead of dd+hh:mm text...
    1 Posts | Last post October 15, 2013
    • I made the following changes to the script so it uses a timespan object for the IdleTime property, allowing the user to do a Where-Object.
      
      Add the following lines above foreach ($Computer in $ComputerName)...
      
        Function Convert-IdleTimeStringToTimeSpan {
          param(
            [CmdletBinding()]
            [Parameter(Mandatory=$true,ValueFromPipeline=$false)]
            [String]$IdleTime
          )
        
          Begin {
            $Days, $Hours, $Minutes = 0, 0, 0
          }
      
          Process {
            If ( $IdleTime -ne '.' ) {
              If ( $IdleTime -like '*+*' ) {
                $Days, $IdleTime = $IdleTime.Split('+')
              }
            
              If ( $IdleTime -like '*:*' ) {
                $Hours, $Minutes = $IdleTime.Split(':')
              } Else {
                $Minutes = $IdleTime
              }
            }
          }
            
          End {
            New-Timespan -Days $Days -Hours $Hours -Minutes $Minutes
          }
        }
      
      And wrap the values passed to $HashProps.IdleTime in Convert-IdleTimeStringToTimeSpan like this...
      
            $HashProps.IdleTime = Convert-IdleTimeStringToTimeSpan($CurrentLine[3])
      
      and 
      
            $HashProps.IdleTime = Convert-IdleTimeStringToTimeSpan($CurrentLine[4])
      
  • error handling when there are no RDP sessions
    2 Posts | Last post October 15, 2013
    • When there are no sessions on the server being queried, QUSER returns:
      "No User exists for *"
      
      easily handled, I just wanted to point it out.
    • It looks like the message "No User exists for *" comes thru on STDERR, so it is not handled by the Select-Object nor ForEach-Object.
21 - 29 of 29 Items