Jan 17 2012 3:00PM GMT
Posted by: Richard Siddaway
PowerShell v2, Users, WMI
Get the logged on users
Posted by: Richard Siddaway
Do you know which users are logged on to your systems?
Want to find out?
function get-logedonuser { param ( [string]$computername = $env:COMPUTERNAME ) Get-WmiObject -Class Win32_LogonSession -ComputerName $computername | foreach { $data = $_ $id = $data.__RELPATH -replace """", "'" $q = "ASSOCIATORS OF {$id} WHERE ResultClass = Win32_Account" Get-WmiObject -ComputerName $computername -Query $q | select @{N="User";E={$($_.Caption)}}, @{N="LogonTime";E={$data.ConvertToDateTime($data.StartTime)}} } }
Use the Win32_LogonSession class and then find the associated Win32_Account classes. It does work for domain and local accounts




