Get Active Directory user account last logged on time (VBScript)

Introduction

This VBScript shows how to use Windows PowerShell to determine the last time that a user logged on to the system.

Scenarios

In some cases, IT admins want to statistics of user last logon time information. This Windows PowerShell script will help IT admins to determine the last time that a user logged on to the system.

Script

Step 1: Click Start, type cmd in the search box on the Start Menu, right-click the cmd.exe icon, and then click Run as administrator. If the User Account Control dialog box appears, confirm that the action it displays is what you want, and then click Continue.

Step 2: Run this VBScript with cscript.exe in the Windows Console (type the name of the script at the command prompt)
 

Step 3: If the script runs successfully, you will see something as shown in the following figure.

Here are some code snippets for your references.

VB Script
Edit|Remove
'Set query attribute. 
strBase = "<LDAP://" & strDomain & ">" 
strFilter = "(&(objectCategory=person)(objectClass=user))" 
strAttributes = "distinguishedName,lastLogonTimeStamp" 
  
Const ADS_SCOPE_SUBTREE=2 
ADOCommand.CommandText = strBase & ";" & strFilter & ";" & strAttributes & ";subtree" 
ADOCommand.Properties("Page Size") = 1000 
ADOCOmmand.Properties("Searchscope") = ADS_SCOPE_SUBTREE 
Set ADORecordset = ADOCommand.Execute 
  
'Get local time zone from registry 
Set objShell = CreateObject("Wscript.Shell") 
regKey = objShell.RegRead("HKLM\System\CurrentControlSet\Control\TimeZoneInformation\ActiveTimeBias") 
  
If (UCase(TypeName(regKey)) = "LONG") Then 
    reg = regKey 
ElseIf (UCase(TypeName(regKey)) = "VARIANT()") Then 
    reg = 0 
    For i = 0 To UBound(regKey) 
        reg = reg + (regKey(i) * 256^i) 
    Next 
End If

Prerequisite

Windows Server 2008 or higher version