Articles > Windows 2003
Printer Friendly Version
Views: 7875

How to find unused local user accounts on Windows Server

Last Updated: 4/6/10

' Run the code
get_Last_Logons()

' Return all users who have never logged in or last logged in more that 90 days ago
Sub get_Last_Logons()
on error resume next

      Dim oOutput      ' The file to write the output to
      Dim colUsers      ' Collection of local users on the server
      Dim strLastLogin ' String to hold the last login date if found

      ' Create the output file
      Set oOutput = CreateObject("Scripting.FileSystemObject").OpenTextFile("c:\output.txt",2,True)

      ' Get the servers local users
      Set colUsers = GetObject("WinNT://" & CreateObject("Wscript.Network").ComputerName)
      colUsers.Filter = Array("user")

      ' For each user in the collection
      For Each oUser In colUsers
            ' Get the last login property
            strLastLogin = oUser.LastLogin

            ' If an error occured then output that the last login was not found
            If err.number <> 0 Then
                  err.Clear
                  oOutput.WriteLine oUser.Name & ";NOT FOUND"
            
            ' Else output the user id and the last login time for users who 
last logged in more than 90 days ago
            ElseIf dateDiff("d",strLastLogin,Date) > 90 Then
                    oOutput.WriteLine oUser.Name & ";" & strLastLogin
            End If
      Next
      
      ' Tidy up
      oOutput.Close
      Set oOutput = Nothing

End Sub





Keywords: find unused logins lastlogon local user accounts windows server 2003