Windows IT Pro is the authoritative and independent resource for windows nt, windows 2000, windows 2003, windows xp. Features a collection of resources and magazines for windows IT professionals.
  
  
  Advanced Search 



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

RSS
Subscribe to Windows IT Pro | See More VBScript Articles Here | Reprints | Or get the Monthly Online Pass—only $5.95 a month!

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

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

To run type the following:

C:\> cscript userlogin.vbs

Below is a sample output:

C:\>cscript d:\temp\disuser.vbs
Microsoft (R) Windows Scripting Host Version 5.0 for Windows
Copyright (C) Microsoft Corporation 1996-1997. All rights reserved.

Domain : SAVILLTECH
Full Name=Maria Aala (DIS 120 inactive)Last login=27/05/1999 14:44:24
Full Name=Paul J AaronLast login=16/08/1999 13:01:56
Full Name=Hany A AbbasLast login=23/08/1999 13:25:46
Full Name=Tony S AbbittLast login=27/08/1999 15:07:20
Full Name=Adnan AbdallahLast login=16/07/1999 10:34:58
Full Name=Tony AbelaLast login=21/07/1999 10:43:20
Full Name=Juan Claudio AbelloLast login=25/06/1999 11:15:32
Full Name=Marie J B AbreyLast login=07/09/1999 08:00:34
Full Name=Philippa AbsilLast login=07/09/1999 06:33:18
Full Name=Ace Test account for NetID - Alistair PurvisLast login=28/01/1999 07:5
4:30
Full Name=Chris AckermannLast login=07/09/1999 08:21:20
Full Name=Curtis S AdamsLast login=10/08/1999 12:32:02
Full Name=Helen R Adams DIS user left 27.8.99Last login=02/08/1999 08:52:58
Full Name=Michael Adams Dis 4.6.99 NMFLast login=03/06/1999 08:50:10
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.


End of Article



Reader Comments
>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).

Sure. Save the following script like usrlogin.vbs

---8<---
On Error Resume Next
Set oDomain = GetObject("WinNT://" & WScript.Arguments(0))
oDomain.Filter = Array("User")
WScript.Echo "Showing last login times of accounts from: " & oDomain.Name & vbCRLF
For Each oDItem In oDomain
Err.Clear
sULogin = oDItem.LastLogin
For nI = 1 To (WScript.Arguments.Count - 1)
Set oUsr = GetObject("WinNT://" & WScript.Arguments(nI) & "/" & oDItem.Name & ",user")
If oUsr.LastLogin > sULogin Then sULogin = oUsr.LastLogin
Next
If Err.Number = 0 Then WScript.Echo "Username: " & Left(oDItem.Name & Space(22),22) & "Last login: " & FormatDateTime(sULogin)
Next
---8<---

and execute it from the command line like

---8<---
C:\>cscript d:\temp\usrlogin.vbs //nologo PDC BDC1 BDC2
Showing last login times of accounts from: PDC

Username: dummyno1 Last login: 2001-03-17 13:59:10
Username: dummyno2 Last login: 2001-03-06 14:57:39
Username: dummyno3 Last login: 2001-03-24 15:26:32
Username: dummyno4 Last login: 2001-03-26 15:26:32
Username: dummyno5 Last login: 2001-03-17 08:21:51
Username: dummyno6 Last login: 2001-03-15 15:06:18
Username: dummyno7 Last login: 2001-03-27 15:46:10
Username: dummyno8 Last login: 2001-03-13 15:49:42
Username: dummyno9 Last login: 2001-03-26 10:18:54
---8<---

Change "PDC BDC1 BDC2" with a list of the names of your DCs, starting with the PDC. If the first argument isn't the PDC (or domain) name, some accounts may be left out of the list. Only accounts which have been logged in to at least once will be shown.


Best Regards,

Jakob Hussfelt

Jakob Hussfelt April 07, 2001


Your Comments (required): good program for big network. thanks to the programer.

anuj kumar June 28, 2004


Excellent - thanks.

Anonymous User January 18, 2005 (Article Rating: )


Will the same script work efficiently in Windows 2000/2003 as well ?

hellonavneet@yahoo.com February 17, 2005


QUITE GOOD but can you improve by making all last login users date/time on a word doc or because after open with cmd, once after the last user it automatically exits so it would be better to put all into a notepad where by I can view all last user login @ my own pace.

because my network is a big big 1 has nearly 1k users. After viewing the result through notepad, when i close it, it will automatically delete the file

so in total suggestions
-echo all lastuserlogin details in notepad
-delete file after the user has close the file
ya thats all thank you very much for such a wonderful programme

Anonymous User June 01, 2005 (Article Rating: )


You can pipe it to a file.
C:\> cscript userlogin.vbs > lastlogon.txt
You can then open with Excel.


Anonymous User June 01, 2005 (Article Rating: )


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

redBlue November 13, 2005 (Article Rating: )


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.

alexandresumare October 31, 2006 (Article Rating: )


You must log on before posting a comment.

If you don't have a username & password, please register now.




Top Viewed ArticlesView all articles
Friday at PASS Europe 2006

Kevin talks about the closing day of the event and shares a funny Microsoft film. ...

Escape From Yesterworld

Kevin points you to the funniest SQL Server website ever! ...

The Desktop tab is missing from the Display Properties in Windows XP?

...


Windows OSs Whitepapers Replay for Exchange: Enterprise Protection and an Affordable Price

Are You Satisfied?

A Preliminary Look at Deployment Plans for Microsoft Windows Vista

Related Events Check out our list of Free Email Newsletters!

Windows OSs eBooks Understanding and Leveraging Code Signing Technologies

A Guide to Windows Certification and Public Keys

SQL Server Administration for Oracle DBAs

Related Windows OSs Resources Become a VIP member of the Windows IT Pro community!
Get it all with the VIP CD and VIP access. A $500+ value for only $279!

Subscribe to Windows IT Pro!
Solve your toughest technical problems with our experts and access 10,000 + articles online. 30% off

Monthly Online Pass - Only $5.95!
Get instant access to 10,000+ articles from Windows IT Pro Magazine!

TechNet Virtual Labs
Evaluate and test Microsoft's newest products.

Job Openings in IT


ADS BY GOOGLE SPONSORED LINKS FEATURED LINKS

WinConnections Conference Fall 2008
Don’t miss the premier event for Microsoft IT Professionals in Las Vegas, November 10-13. Register and book your room by August 25 and receive a FREE room night (based on a three night minimum stay).

Maximize your SharePoint Investment – 8 Cities
Discover best practices and tips for both architecting and administering SharePoint. Early Bird Price of $99 through Sept 15th.

Find a new job now on the all new IT Job Hound!
Search jobs, post your resume, and set up job e-mail alerts!

Master SharePoint with 3 eLearning Seminars
Learn how to build a better SharePoint infrastructure and enable powerful collaboration with MVPs Dan Holme and Michael Noel. Register today!

Top Tools for Virtualization Disaster Recovery & Replication
View this web seminar on August 14th to learn about two tools that will result in faster backup and restore with P2V disaster recovery.

SharePointConnections Conference Fall 2008
Don’t miss the premier event for Microsoft IT Professionals in Las Vegas, November 10-13. Register and book your room by August 25 and receive a FREE room night (based on a three night minimum stay).

VMworld 2008 - Sign Up Today!
Join your peers on September 15-18 at The Venetian Hotel in Las Vegas as VMware hosts VMworld 2008, the leading Virtualization event.



When managing just VMware isn’t enough
Plan/Manage/Secure – NetIQ VMware management. Download whitepaper.

What’s up with your network? Find out with ipMonitor
Availability monitoring for servers, applications and networks – FREE trial

Microsoft® Tech•Ed EMEA 2008 IT Professionals
Advance your thinking with new ideas and practical real-world solutions at Microsoft’s FIVE day technical infrastructure conference 3-7 Nov., 2008. Register before 26 September 2008 to save €300.

Order Your Fundamentals CD Today!
Gain an introduction to Exchange, learn server security requirements, and understand how unified communications can play a role in your messaging strategies with this free Exchange CD.

Are You Really Compliant with Software Regulations?
View this web seminar that will help you with compliance best practices and check out a management solution to assure that you won’t be in jeopardy of an audit.

Virtualization Congress Oct. 14-16 in London
Don't miss Virtualization Congress, the premiere EMEA conference dedicated to hardware, OS and application virtualization. Oct. 14-16 in London.
Windows IT Pro Home Register FAQ for Windows WinInfo News
Europe Edition About Us Contact Us/Customer Service Media Kit Affiliates / Licensing  
SQL Server Magazine Office & SharePoint Pro Windows Dev Pro IT Job Hound ITTV
IT Library Technical Resources Directory Connected Home Windows Excavator Windows SuperSite 
 
 Windows IT Pro is a Division of Penton Media Inc.
 Copyright © 2008 Penton Media, Inc., All rights reserved. Terms and Use | Privacy Statement | Reprints and Licensing