In a previous tip, you learned how to determine who the user is logged onto the physical machine. However, this will not discover logged-on terminal service users or users inside virtual machines. You must use the code below to find those:
$computername = 'localhost'
Get-WMIObject Win32_Process –filter 'name="explorer.exe"' -computername $ComputerName |
ForEach-Object { $owner = $_.GetOwner'{0}\{1}' -f $owner.Domain, $owner.User } |
Sort-Object | Get-Unique