Search Windows and Linux Networking

Friday, November 15, 2013

How to Search All Disabled User In Active Directory.

How to Search all Disabled User In Active Directory and export in to the Excel File using VBScript.


OR

 How to Search all Active/Enabled User In Active Directory and export in to the Excel File using VBScript


There are some time when you needed or your manager ask to provide all Disabled user Or currently all Active User from Active Directory in excel file and there are more than 100 Accounts in your Active Directory and you have to give all information in limited time frame. then best option is to use VbScript that search all disabled or All Active/Enabled users in Active Directory.

Following is the VBScript that Search for all Disabled Users in entire domain.
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

 Option Explicit
Dim objRootDSE,strDNSDomain,adoConnection,strQuery,adoRecordset,Field
Dim CN,FirstName,LastName,initials,Descrip,EmailAddr
Dim strFileName, ObjExcel, intRow

'Search all Disabled users in Active Directory and export them in to Excel file.


wscript.echo "Searching all disabled users in Active Directory please wait...."

'Open Excel file to Write the Data

Set ObjExcel = CreateObject("Excel.Application")
objExcel.Visible = True
objExcel.WorkBooks.Add()
intRow = 1
       ObjExcel.Cells(intRow,1).Value = "CN"
       ObjExcel.Cells(intRow,2).Value = "FirstName"
       ObjExcel.Cells(intRow,3).Value = "initials"
       ObjExcel.Cells(intRow,4).Value = "LastName"
       ObjExcel.Cells(intRow,5).Value = "EmailAddr "

' Bind to Domain or OU

Set objRootDSE = GetObject("LDAP://RootDSE")
strDNSDomain = objRootDSE.Get("defaultNamingContext")
Set adoConnection = CreateObject("ADODB.Connection")
adoConnection.Provider = "ADSDSOObject"
adoConnection.Open "ADs Provider"
strQuery = "<LDAP://" & strDNSDomain & ">;(&(objectCategory=person)(objectClass=user));adspath;subtree"

'Define condition for search
Set adoRecordset = adoConnection.Execute(strQuery)

' Loop to search users through them...


Do While Not adoRecordset.EOF
     Set Field = GetObject(adoRecordset.Fields(0).Value)
     If Field.accountDisabled = TRUE Then
       'wscript.echo "Account " & Field.displayname & " is disabled"
       intRow = intRow + 1
       Cn = Field.CN
       FirstName = Field.GivenName
       LastName = Field.sn
       initials = Field.initials
       Descrip = Field.description
       EmailAddr = Field.mail

       'Wscript.echo "Users full Name is " & FirstName & " " & initials & " " & LastName & " and his Email ID is " & EmailAddr

       ' Write to Excel File
       ObjExcel.Cells(intRow,1).Value = CN
       ObjExcel.Cells(intRow,2).Value = FirstName
       ObjExcel.Cells(intRow,3).Value = initials
       ObjExcel.Cells(intRow,4).Value = LastName
       ObjExcel.Cells(intRow,5).Value = EmailAddr
     
      End If
 
adoRecordset.MoveNext

Loop
'If completed notify as it done.
wscript.echo "Done"

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++





You can also use this script to search all Active/Enabled user in entire domain by just modifying condition Field.accountDisabled = TRUE with Field.accountDisabled = FALSE

OR

You can also use this script to find all Active/Disalbed users by just modifying Bind base string (strQuery ) using Organization Unit. For example

strQuery = "<LDAP://OU=DisabledAccounts,DC=Example,DC=com>;(&(objectCategory=person)(objectClass=user));adspath;subtree"

I hope this will help you. :-)