How to Find the Last Logon Date/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 logged in date and time for all user accounts on the computer.

Find the last login date/time for all user accounts

Important: For Windows 10 Microsoft Account (MSA) accounts, the last login information showed by the script, Net command-line, or PowerShell methods below 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.

Method 1: Using a VBScript

Here is a VBScript that I came up with, that displays the last login date/time details for each local user account on the 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. It 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. Create a shortcut to the VBScript file in the Startup folder.




Method 2: Using PowerShell

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


Method 3: Using the NET USER command-line

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


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. It won't take more than 10 seconds of your time. The share buttons are right below. :)

Ramesh Srinivasan is passionate about Microsoft technologies and he has been a consecutive ten-time recipient of the Microsoft Most Valuable Professional award in the Windows Shell/Desktop Experience category, from 2003 to 2012. He loves to troubleshoot and write about Windows. Ramesh founded Winhelponline.com in 2005.

4 thoughts on “How to Find the Last Logon Date/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 Reply to Rajsekhar Cancel reply