header image

Find the logged on user

Posted by: | June 12, 2017 Comments Off on Find the logged on user |

One method of finding the logged on users is to use CIM

$ComputerName = $env:COMPUTERNAME

Get-CimInstance -ClassName Win32_Process -ComputerName $ComputerName -Filter "Name = 'explorer.exe'" | 
foreach { 
 $lguser = Invoke-CimMethod -InputObject $psitem -MethodName GetOwner 
 $Properties = @{ 
 ComputerName = $ComputerName 
 User = $lguser.User 
 Domain = $lguser.Domain 
 Time = $User.CreationDate 
 New-Object -TypeName PSObject -Property $Properties 

Get the Win32_Process instances for explorer.exe and foreach of them use the GetOwner method to get the owners names and domain. Create an object and ouput


under: PowerShell and CIM, PowerShell and WMI

Comments are closed.