Find Last Logon Date and Time for Local User Accounts in Windows

In response to a script request recently from one of our readers, here is an article that tells you how to find the last logon times for all user accounts in the computer.

VBScript: Find the last login date for all user accounts

Here is a VBScript that I came up with, that displays last login date/time details for each local user account in a computer.

Copy the following lines of code to Notepad, and save the file as last_logon.vbs

Option Explicit
Dim objWMIService, colItems, WshNetwork, strComputer
Dim objUser, objItem, dtmLastLogin, strLogonInfo
Set WshNetwork = CreateObject("Wscript.Network")
strComputer = WshNetwork.ComputerName

Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery _
("Select * from Win32_UserAccount Where Domain = '" & strComputer & "'")

For Each objItem in colItems
	dtmLastLogin = ""
	On Error Resume Next
	Set objUser = GetObject("WinNT://" & strComputer _
    	& "/" & objItem.Name & ",user")
	dtmLastLogin = objUser.lastLogin
	On Error Goto 0
	strLogonInfo = strLogonInfo & vbCrLf & objItem.Name & ": " & dtmLastLogin
Next

MsgBox strLogonInfo, vbOKOnly + vbInformation, "Last Logon Information for Local Users"

Double-click the script to run it. Wait for 30-40 seconds to see the list of user accounts and the corresponding “last login” times.

last logon date for all user accounts local computer - vbscript

This script uses WMI’s Win32_UserAccount class to get the list of local user account information, queries the LastLogin property for each local user account and displays it in a message box.

Tip: If you need to know the last login information of all user accounts at every startup, place the script into your Startup folder. To open the Startup folder of your user profile, press Win + R to access the Run dialog, type shell:startup and click OK. Drag the VBScript file to the Startup folder.

PowerShell: Find the last login date/time for all user accounts

To find the last login information for all local accounts using PowerShell, run one of the following commands in the PowerShell window:



Get-LocalUser |Select Name, Lastlogon

(or)

$([ADSI]"WinNT://$env:COMPUTERNAME").Children | where {$_.SchemaClassName -eq 'user'} | Select Name, Lastlogin

last logon date for all user accounts local computer

NET USER command-line to find the last login date for all user accounts

The NET USER <USERNAME> command shows the last login time of a user account.

last logon date for all user accounts local computer - net user

Note: For Windows 10 Microsoft Account (MSA) accounts, the last login information showed by the script, Net command-line, or PowerShell won’t match the actual last logon time. That’s because once you switch from a local user account to MSA, Windows won’t consider it as a local account.


One small request: If you liked this post, please share this?

One "tiny" share from you would seriously help a lot with the growth of this blog. Some great suggestions:
  • Pin it!
  • Share it to your favorite blog + Facebook, Reddit
  • Tweet it!
So thank you so much for your support, my reader. It won't take more than 10 seconds of your time. The share buttons are right below. :)

About the author

Ramesh Srinivasan founded Winhelponline.com back in 2005. He is passionate about Microsoft technologies and he has been a Microsoft Most Valuable Professional (MVP) for 10 consecutive years from 2003 to 2012.

3 thoughts on “Find Last Logon Date and Time for Local User Accounts in Windows”

  1. This script is fantastic, however, it is not pulling all of my users. I have a list of about 30 local users, but about 9 are not being listed. Any ideas on how I can get the others to populate?

    Reply

Leave a Comment