Advertisement
PureGremlin

Get-Loggedinuser

Dec 6th, 2021
1,409
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Function Get-LoggedInuser {
  2.  
  3. <#
  4.     .SYNOPSIS
  5.         Shows all the users currently logged in to specified or remote computers
  6.  
  7.     .DESCRIPTION
  8.         Shows the users currently logged into the specified computername if not specified local computers will be shown.
  9.  
  10.     .PARAMETER ComputerName
  11.         One or more computernames
  12.  
  13.     .EXAMPLE
  14.         PS C:\> Get-LoggedInUser
  15.         Shows the users logged into the local system
  16.  
  17.     .EXAMPLE
  18.         PS C:\> Get-LoggedInUser -Computer server1,server2,server3
  19.         Shows the users logged into server1, server2, and server3
  20.  
  21.     .EXAMPLE
  22.         PS C:\> Get-LoggedInUser  | where{$_.sessionname -match 'RDP'}  ## shows where users are connected VIA RDP
  23.  
  24.     .Example
  25.  
  26.         PS C:\> Get-ADComputer -Filter *|select -exp name |Foreach{Get-LoggedInUser -Computer $_ }
  27.         Shows all computers in active directory computers
  28.  
  29.     .Example
  30.  
  31.         PS C:\> get-content C:\listofcomputers.txt |Foreach{Get-LoggedInUser -Computer $_ }
  32.         Shows all computers in the text file.
  33.  
  34.     .Example
  35.        
  36.         PS C:\> Get-LoggedInUser -computer server1,server2  | where{$_.username -eq 'admin'}
  37.        Shows where a user islogged in to.
  38.  
  39.     .MORE
  40.          [PSCustomObject]@{
  41.           ComputerName    = $Comp
  42.           Username        = $line.SubString(1, 20).Trim()
  43.           SessionName     = $line.SubString(23, 17).Trim()
  44.           #ID             = $line.SubString(42, 2).Trim()
  45.           State           = $line.SubString(46, 6).Trim()
  46.           #Idle           = $line.SubString(54, 9).Trim().Replace('+', '.')
  47.           #LogonTime      = [datetime]$line.SubString(65)
  48.  
  49.           Remove # Against each if you want to see the Output of the Value
  50.      
  51. ************************************************************************
  52.  Notes: Run as administrator
  53.  Author: Jiten https://community.spiceworks.com/people/jitensh
  54. Date Created: 08/30/2018
  55.      Credits:
  56. Last Revised: 08/30/2018
  57. ************************************************************************
  58. #>
  59.  
  60. [cmdletbinding()]
  61.    param(
  62.    [String[]]$Computer = $env:COMPUTERNAME
  63.    )
  64.  
  65.    ForEach ($Comp in $Computer)
  66.    {
  67.    If (-not (Test-Connection -ComputerName $comp -Quiet -Count 1 -ea silentlycontinue))
  68.    {
  69.    Write-Warning "$comp is Offline"; continue
  70.    }
  71.    $stringOutput = quser /server:$Comp 2>$null
  72.       If (!$stringOutput)
  73.       {
  74.       Write-Warning "Unable to retrieve quser info for `"$Comp`""
  75.       }
  76.       ForEach ($line in $stringOutput){
  77.          If ($line -match "logon time")
  78.          {Continue}
  79.  
  80.          [PSCustomObject]@{
  81.           ComputerName    = $Comp
  82.           Username        = $line.SubString(1, 20).Trim()
  83.           SessionName     = $line.SubString(23, 17).Trim()
  84.           ID             = $line.SubString(42, 2).Trim()
  85.           State           = $line.SubString(46, 6).Trim()
  86.           #Idle           = $line.SubString(54, 9).Trim().Replace('+', '.')
  87.           #LogonTime      = [datetime]$line.SubString(65)
  88.           }
  89.          
  90.       }
  91.    }
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement