Site icon Winhelponline

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

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.

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


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

Method 3: Using the NET USER command-line

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

Exit mobile version