Script Center > Gallery > Active Directory > Export Active Directory User Information
TechNet Script Center logo

Welcome to the TechNet Script Center Gallery!

Each contribution is licensed to you under a License Agreement by its owner, not Microsoft. Microsoft does not guarantee the contribution or purport to grant rights to it.

Export Active Directory User Information

(Community)
Rate it:
 
 
 
 
 
Script Code
VBScript
'* description: export users from Active Directory to a comma separated text file.
'*  Use the text file to create users in a new AD with the accompanying import script.
'*              
'* author: Chris Pilling
'* date: 18 June 2008
'* edit the attributes exported to suit
'* after running it is probably best to delete lines from the text file for system generated accounts
 
Const ForAppending = 8
 
Set objRoot = GetObject("LDAP://RootDSE") 
strDNC = objRoot.Get("DefaultNamingContext") 
Set objDomain = GetObject("LDAP://" & strDNC)
 
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.CreateTextFile("C:\AD_Users_Export.txt")
 
Call enummembers(objDomain) 
Sub enumMembers(objDomain) 
On Error Resume Next 
 
For Each objMember In objDomain  
 
If ObjMember.Class = "user" Then
 
'* edit the attributes you want to export here  
 
If Not (isempty(ObjMember.samAccountName)) Then 
    SamAccountName = ObjMember.samAccountName 
else 
    SamAccountName = "" 
End If
If Not (isempty(ObjMember.CN)) Then Cn = ObjMember.CN else Cn = "" End If
If Not (isempty(objMember.GivenName)) Then FirstName =objMember.GivenName else FirstName = "" End If
If Not (isempty(objMember.sn)) Then Lastname = ObjMember.sn else LastName = "" End If
If Not (isempty(objMember.UserPrincipalName)) Then 
    UserPrincipalName = objMember.UserPrincipalName 
    else 
    Name = "" 
End If
If Not (isempty(objMember.ProfilePath)) Then 
    ProfilePath = objMember.profilePath 
else 
    Profile = "" 
End If
If Not (isempty(ObjMember.homeDrive)) Then homeDrive = ObjMember.homeDrive else homeDrive = "" End If
If Not (isempty(ObjMember.homeDirectory)) Then 
    homeDirectory = ObjMember.homeDirectory 
else 
    homeDirectory = "" 
End If
If Not (isempty(ObjMember.Scriptpath)) Then 
    LoginScript = ObjMember.Scriptpath 
else 
    LoginScript = "" 
End If
 
set objFolder = nothing
set objFile = nothing
 
Set objTextFile = objFSO.OpenTextFile ("d:\AD_Users_Export.txt", ForAppending, True)
 
wscript.echo SamAccountName & "," & CN & "," & FirstName & "," & LastName & "," & UserPrincipalName & _
    "," & ProfilePath & ","  Homedrive & "," & Homedirectory & "," & LoginScript
 
strText1 = SamAccountName & "," & CN & "," & FirstName & "," & LastName & "," & UserPrincipalName & _
    "," & ProfilePath & "," & homedrive & "," & Homedirectory & "," & LoginScript

objTextFile.WriteLine(strText1) 
objTextFile.Close
 
End If 
  
If objMember.Class = "organizationalUnit" or OBjMember.Class = "container" Then 
enumMembers (objMember) 
End If 
Next 
End Sub
Platforms
Windows Server 2008 R2 No
Windows Server 2008 No
Windows Server 2003 No
Windows 7 No
Windows Vista No
Windows XP No
Windows 2000 No
For online peer support, join The Official Scripting Guys Forum! To provide feedback or report bugs in sample scripts, please start a new discussion on the Discussions tab for this script.
Disclaimer The sample scripts are not supported under any Microsoft standard support program or service. The sample scripts are provided AS IS without warranty of any kind. Microsoft further disclaims all implied warranties including, without limitation, any implied warranties of merchantability or of fitness for a particular purpose. The entire risk arising out of the use or performance of the sample scripts and documentation remains with you. In no event shall Microsoft, its authors, or anyone else involved in the creation, production, or delivery of the scripts be liable for any damages whatsoever (including, without limitation, damages for loss of business profits, business interruption, loss of business information, or other pecuniary loss) arising out of the use of or inability to use the sample scripts or documentation, even if Microsoft has been advised of the possibility of such damages.
Be the first to create a discussion.