Powershell script to find if a user has logged in since last reboot

Solution 1:

If you're set on using WMI, you can get LastBootUpTime a bit cleaner:

PS C:\> $wmi = Get-WmiObject -Class Win32_OperatingSystem
PS C:\> $rebootTime = $wmi.ConvertToDateTime($wmi.LastBootUpTime)
PS C:\> $rebootTime
Tuesday, May 24, 2011 3:18:28 PM

Taking that, we search the Security Event Log, since $rebootTime, for the most recent, successful, eventID 4624 containing Logon Type 2 -- Interactive Logon:

PS C:\> $entry = Get-EventLog -After $rebootTime -LogName Security | Where-Object {($_.EventID -eq '4624') -and ($_.EntryType -eq 'SuccessAudit') -and ($_.Message | Select-String "Logon Type:\t\t\t2")} | Select-Object -First 1
PS C:\> $lastLogon = $entry.TimeGenerated
PS C:\> $lastLogon
Tuesday, May 24, 2011 3:19:34 PM

Then, a quick compare:

PS C:\> $lastLogon -gt $rebootTime
True

The above code can be dumped into a script and/or executed on a remote computer. I only ran the commands interactively to demonstrate the example output.