Get computer's last user logon

Hey everyone I have a little inventory script and I would like to add what the last user is who used a certain machine. I know you can query AD and find what user logged on where but I would like to query a single machine and find the last user of that machine. I haven't been able to find a good way to do this. I know I could find this info in the event viewer under security or I could find which user account folder was the newest, but I would probably have to do some filtering with both of these methods. So what's the best way to go about doing this?

Thanks!!


  • Edited by Cody_Horton Monday, February 16, 2015 4:25 PM
February 16th, 2015 7:25pm

Okay thanks a lot. Unfortunately I'm not to familiar with vbs. However I saw a link for this site in the comments that said how to do it : http://blogs.technet.com/b/heyscriptingguy/archive/2012/02/19/use-powershell-to-find-last-logon-times-for-virtual-workstations.aspx

This is what I ended up with

$Computer = "cats2552220x003"
$Win32OS = Get-WmiObject -Class Win32_OperatingSystem -ComputerName $Computer
$Build = $Win32OS.BuildNumber
If ($Build -ge 6001){
    $Win32User = Get-WmiObject -Class Win32_UserProfile -ComputerName $Computer
    $Win32User = $Win32User | Where-Object {($_.SID -notmatch "^S-1-5-\d[18|19|20]$")}
    $Win32User = $Win32User | Sort-Object -Property LastUseTime -Descending
    $LastUser = $Win32User | Select-Object -First 1
    $Loaded = $LastUser.Loaded
    $Time = ([WMI] '').ConvertToDateTime($LastUser.LastUseTime)
    $UserSID = New-Object System.Security.Principal.SecurityIdentifier($LastUser.SID)
    $User = $UserSID.Translate([System.Security.Principal.NTAccount])

    $Computer
    $User.Value
    $Time
    $Loaded
}
Thanks again!

 
February 17th, 2015 4:55pm

You're welcome, and sorry about foisting vbscript on you! Glad you looked closely and found that powershell solution!

Free Windows Admin Tool Kit Click here and download it now
February 20th, 2015 7:24am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics