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

Advertisements
This entry was posted in PowerShell and CIM, PowerShell and WMI. 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 )

Google+ photo

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

Connecting to %s