Subscribe to Windows IT Pro

 

Get Newsletters

  • Get the Latest News
  • Product Updates
  • Helpful Tricks
  • Productivity Tips

Subscribe Now!

September 18, 1999 12:00 AM

How can I generate a list of last user logon times for a domain?

Windows IT Pro
InstantDoc ID #15113
Rating: (7)

A. I've written a small bit of VBScript which using the Windows Scripting engine can be used to generate a list of all user last logons (if the last logon time is not available the user will be omitted). You will need Windows Scripting Host installed, see 'What is the Windows Scripting Host' for more information.

Save the following text into file userlogin.vbs

' List last logon times<br>
' 2001-03-27 John Savill, Jakob Hussfelt http://www.ntfaq.com<br>
On Error Resume Next<br>
sEnterDCs = "SAVILLTECH,SAVILLNT02"<br>
sObjects = Split(sEnterDCs, ",")<br>
Set oDomain = GetObject("WinNT://" &amp; sObjects(0))<br>
oDomain.Filter = Array("User")<br>
WScript.Echo "Showing last login times of accounts from: " &amp; oDomain.Name &amp; 
vbNewLine<br>
For Each oDomainItem In oDomain<br>
&#160; sUsrLogin = oDomainItem.LastLogin<br>
&#160; If UBound(sObjects) &gt;= 1 Then<br>
&#160;&#160;&#160; For ii = 1 To UBound(sObjects)<br>
&#160;&#160;&#160;&#160;&#160; Set oUsr = GetObject("WinNT://" &amp; sObjects(ii) &amp; 
"/" &amp; oDomainItem.Name &amp; ",user")<br>
&#160;&#160;&#160;&#160;&#160; If oUsr.LastLogin &gt; sUsrLogin Then sUsrLogin = 
oUsr.LastLogin<br>
&#160;&#160;&#160; Next<br>
&#160; End If<br>
&#160; WScript.Echo "Username: " &amp; Left(oDomainItem.Name &amp; Space(22),22) &amp; "Last 
login: " &amp; FormatDateTime(sUsrLogin)<br>
Next

In line 'set oDomain = GetObject("WinNT://SAVILLTECH")' you should change SAVILLTECH to your domain name.

To run type the following:

C:\&gt; <b>cscript userlogin.vbs</b>

Below is a sample output:

C:\&gt;cscript d:\temp\disuser.vbs<br>
Microsoft (R) Windows Scripting Host Version 5.0 for Windows<br>
Copyright (C) Microsoft Corporation 1996-1997. All rights reserved.<br><br>
Domain : SAVILLTECH<br>
Full Name=Maria Aala (DIS 120 inactive)Last login=27/05/1999 14:44:24<br>
Full Name=Paul J AaronLast login=16/08/1999 13:01:56<br>
Full Name=Hany A AbbasLast login=23/08/1999 13:25:46<br>
Full Name=Tony S AbbittLast login=27/08/1999 15:07:20<br>
Full Name=Adnan AbdallahLast login=16/07/1999 10:34:58<br>
Full Name=Tony AbelaLast login=21/07/1999 10:43:20<br>
Full Name=Juan Claudio AbelloLast login=25/06/1999 11:15:32<br>
Full Name=Marie J B AbreyLast login=07/09/1999 08:00:34<br>
Full Name=Philippa AbsilLast login=07/09/1999 06:33:18<br>
Full Name=Ace Test account for NetID - Alistair PurvisLast login=28/01/1999 07:5<br>
4:30<br>
Full Name=Chris AckermannLast login=07/09/1999 08:21:20<br>
Full Name=Curtis S AdamsLast login=10/08/1999 12:32:02<br>
Full Name=Helen R Adams DIS user left 27.8.99Last login=02/08/1999 08:52:58<br>
Full Name=Michael Adams  Dis 4.6.99 NMFLast login=03/06/1999 08:50:10<br>
Full Name=Philip R AdamsLast login=14/06/1999 12:49:00

The advantage of using VBScript is you can change the code to output exactly what you want but don't mail me asking for help changing it!

In the above example it only checks with the PDC. If you have BDC's these values may be wrong as last logon times are not updated from BDC's to the PDC. You may want to update the script to also query the BDC's and display the latest time (if anyone does this feel free to mail it to me and I'll update the FAQ).

You can also use USRSTAT.EXE from the resource kit.


Related Content:

ARTICLE TOOLS

Comments
  • Raj khanna
    7 months ago
    Jul 06, 2011

    Hello Sir
    Can you help with,
    I want to generate a list of logons of one user (ex. JAMES) showing date and time information of last 30 days. I have no knowledge of programing. I am searching a lot but every where it shows only latest logon of all user in AD.
    Thank You.

  • adamsky
    2 years ago
    Mar 13, 2010

    Hi,

    I have made a script that gets the lastlogons for a 2003 domain (2008 will probably also work). It can be found here:

    http://deludi.nl/blog/vbscript/active-directory/lastlogon-active-directory-vbscript/active-directory-vbscript-to-enumerate-the-last-logon-of-all-ad-users-and-a-lot-of-user-attributes-v2/

    Best regards,

    dirk adamsky

  • Rovan
    2 years ago
    Feb 11, 2010

    Hi thanks for that
    it is only goes back to couple of months or maybe it is a size 76k. Is it possible to exceeded a bit?
    Cheers

  • Alexandre
    6 years ago
    Oct 31, 2006

    To complete the code, is possible to list (enumerate) the BDCs in NT4? So, I'd first list the BDCs and then, get the lastlogon from them.
    Thanks.

  • sebnem
    7 years ago
    Nov 13, 2005

    That's a great script BUT I'm unable to modify according to my need:(.This script lists enable and disable accounts If I'm not mistaken.What if I would like to list ONLY

    -enabled accounts
    -only the users who logged on in the last six months

    Any help would be appricate it.
    Thanks

You must log on before posting a comment.

Are you a new visitor? Register Here

advertisement

advertisement

White Papers

Get your Windows 7 deployment off to the right start by implementing PC lockdown. A locked-down environment is easier and cheaper to support since users are less likely to make unnecessary changes to the core system configuration - read more here!

Essential Guides

Is your iSCSI "lossy"? The reality is that most off-the-shelf Ethernet hardware deployed for iSCSI can lose packets, resulting in slow performance or application downtime. Learn how to assess your current iSCSI infrastructure and engineer an advanced iSCSI SAN infrastructure.

Web Seminars

What's the best way to keep your network safe from malware? In this web seminar, security expert Greg Shields suggests an alternative method to the traditional blacklisting approach that is common with anti-virus and anti-malware solutions.

eLearning Series

We bring the experts direct to you to share their real-world perspective and expertise. During each event, three sessions stream in real time, so you can learn, ask questions, and get solutions.
Upcoming event: Getting the Most with Exchange 2010 with Paul Robichaux

Subscribe to Windows IT Pro!

Windows is a trademark of the Microsoft group of companies. Windows IT Pro is used by Penton Media Inc. under license from owner.