Get Logged On User

by Jan 11, 2011

You can use this code to find out which user is locally logged on a machine:

$computername = 'SomeMachine-or-IP'
Get-WmiObject Win32_ComputerSystem -ComputerName $computername | Select-Object -ExpandProperty UserName

Note that this will always return the user logged on to the physical machine. It will not return terminal service user or users inside a virtual machine. You will need administrator privileges on the target machine. Get-WmiObject supports the -Credential parameter if you must authenticate as someone else.

Twitter This Tip!
ReTweet this Tip!