Description

This script should be useful for Helpdesk or other IT Admins to query remote machines to see who is logged on.
This could also be useful for Remote Desktop Services or Citrix Admins
 
* Note this scripts also returns non system accounts that are running services.
 
Use like:
Get-LoggedOnUser -ComputerName Server123,Server456
Returns:
Computer  LoggedOn
--------      --------
Server123  user1234
Server456  user456
Server456  user5678
Server456  SVC-SQL
  
 
Some Info:
 
(If you choose to use the pipeline) this function expects the parameter $ComputerName to be passed over the pipeline by either Name or Value.
 
If you are using another cmdlet such as get-adcomputer you need to do the following:
  
1) transpose the property name from Name to ComputerName using Select-object to pass by propertyName
  
E.g.
$Comps = Get-ADComputer -prop * -filter * | select Name
 
$Comps | Test-Online -Parameter Name | select-object @{Name="ComputerName";Expression={$_.Name}} | Get-LoggedOnUsers
 
 
2) use expand property to form a simple list and pass by value
E.g.
$Comps = Get-ADComputer -prop * -filter * | select -ExpandProperty Name 

$Comps | Test-Online | Get-LoggedOnUser
#OR if you are not using Test-Online just:
 
$Comps| Get-LoggedOnUser

NB.

You can get Test-Online here:

http://gallery.technet.microsoft.com/scriptcenter/2789c120-48cc-489b-8d61-c1602e954b24
 
 

NB. You will also get accounts running services with this query.

You can use the following script to only get users running the Explorer.exe process

http://gallery.technet.microsoft.com/scriptcenter/Find-the-logged-on-users-1161bd92

 

Script

PowerShell
Edit|Remove
function Get-LoggedOnUser { 
#Requires -Version 2.0             
[CmdletBinding()]             
 Param              
   (                        
    [Parameter(Mandatory=$true, 
               Position=0,                           
               ValueFromPipeline=$true,             
               ValueFromPipelineByPropertyName=$true)]             
    [String[]]$ComputerName 
   )#End Param 
 
Begin             
{             
 Write-Host "`n Checking Users . . . " 
 $i = 0             
}#Begin           
Process             
{ 
    $ComputerName | Foreach-object { 
    $Computer = $_ 
    try 
        { 
            $processinfo = @(Get-WmiObject -class win32_process -ComputerName $Computer -EA "Stop") 
                if ($processinfo) 
                {     
                    $processinfo | Foreach-Object {$_.GetOwner().User} |  
                    Where-Object {$_ -ne "NETWORK SERVICE" -and $_ -ne "LOCAL SERVICE" -and $_ -ne "SYSTEM"| 
                    Sort-Object -Unique | 
                    ForEach-Object { New-Object psobject -Property @{Computer=$Computer;LoggedOn=$_} } |  
                    Select-Object Computer,LoggedOn 
                }#If 
        } 
    catch 
        { 
            "Cannot find any processes running on $computer" | Out-Host 
        } 
     }#Forech-object(Comptuters)        
             
}#Process 
End 
{ 
 
}#End 
 
}#Get-LoggedOnUser