Select Page

Good day, friends:

Today, I spent waaaaaay too much time trying to figure out the answer to a simple question of “when you query WMI for the logged on user and no one is logged on, is the result <blank> or <null>?”

Simple answer is “Null.” And here’s the stupid PS quickie I used.  This will allow you to determine whether someone is logged on and, if so, do “that,” or if no one is logged on, do “this.”

$LoggedOnUser = Get-WmiObject -ComputerName <computername> -Class Win32_ComputerSystem 

If ($LoggedOnUser.Username -eq $Null) {

    #Do this
    write-host "No one logged on."

}

Else {

    #Do that
    write-host "$LoggedOnUser is logged on."

}