header image

Finding if the user associated with a profile is logged on

Posted by: | September 4, 2012 | No Comment |

A forum question asked how to find if the user to whom a profile belonged was logged on. There isn’t an easy way as there isn’t an association between the profile and the log on session.

There is a quick and dirty way  though

Get-WmiObject -Class Win32_UserProfile |            
foreach {            
$filt =  Split-Path -Path $($_.LocalPath) -Leaf            
$loggedon = $null            
$loggedon = Get-WmiObject Win32_loggedonuser |             
 where {$_.Antecedent -like "*Name=*$filt*"} |             
 select -First 1             
$log = $false            
if ($loggedon){$log = $true}            
New-Object -TypeName PSObject -Property @{            
 Name = $filt            
 Logged = $log            

get the profiles and for each split the Localpath (path to profile) – the leaf holds the user name

Test if you can find an instance of Win32_LoggedOnUser where the Antecedent contains the name

Display data.  Results look like this

Name           Logged

—-           ——

DefaultAppPool  False

Richard          True

NetworkService  False

LocalService    False

systemprofile   False

need to filter the system accounts out but I’ll leave that to you

under: PowerShell and WMI