Generate employees list from Active Directory

I wrote a script to list all employees (user accounts) from Active Directory in CSV file format. If you need same thing, copy the script to a Notepad and save it a EmployeeList.vbs. Run the script as below in Command Prompt.

CScript //NOLogo EmployeeList.vbs > EmployeeList.csv

This script lists First Name, Last Name and Telephone (desk) of all users. This script avoids user names without telephone number, so it doesn’t list service accounts and such. You can add more details and check the comments in the script. Now the script.

‘Script: Employee List.vbs
‘Purpose: Generate a CSV file of all User accounts

‘Written by: Anand Venkatachalapathy
Dim rootDSE, strContainer
Dim objConnection, objCommand
Dim objRecordSet

‘Find the Domain root container
Set rootDSE=GetObject("LDAP://RootDSE")
strContainer = rootDSE.Get("defaultNamingContext")

‘Create a ADODB connection and ADODB command objects
Set objConnection = CreateObject("ADODB.Connection")
Set objCommand = CreateObject("ADODB.Command")

‘Set the Query settings
objConnection.Provider = ("ADsDSOObject")
objConnection.Open "Active Directory Provider"
objCommand.ActiveConnection = objConnection
objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE

‘If you are expecting more than 3000 entries in the
‘query results, change the value here
objCommand.Properties("Page Size") = 3000   

‘Add more employee details here in this query
‘To find what’s name of the field, open ADSIEdit.msc
‘and Open any user properties. Note down the fields
‘ and add it here in this query
objCommand.CommandText = _
  "SELECT givenName,sn,displayName,telephoneNumber " _
   & "FROM ‘LDAP://" & strContainer & "’ " _
   & "WHERE objectClass=’user’ "

‘Run the query
Set objRecordSet = objCommand.Execute

‘Header..add more headers if you have more
WScript.Echo "First Name,Last Name,Telephone Number"
Do Until objRecordSet.EOF
    If objRecordSet.Fields("telephoneNumber").Value <> "" Then
‘Don’t forget to add all of your fields here
        WScript.Echo objRecordSet.Fields("givenName").Value & "," & objRecordSet.Fields("sn").Value & "," & objRecordSet.Fields("telephoneNumber").Value
    End If

Set objConnection = Nothing
Set objCommand = Nothing

Keywords: Employee Phone List from Active Directory, Phone directory, User List

One thought on “Generate employees list from Active Directory

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s