Get the logged on users

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

Advertisement
This entry was posted in PowerShell and WMI, PowerShell V2. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s