Active Directory Script Get User's Home folder and Terminal Services Profile

I am needing a script to gather all users in an OU and list their Home folder and their Terminal Services Profile in a text file.  With this information i need delete the home folder and Profile from the servers.  Also i need their email address so i can delete them for our external spam/Virus scanning provider.   All users in the OU will be deleted after this information is collected
pmcguireAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
binarykukiConnect With a Mentor Commented:
This definitely works
On Error Resume Next
 
Const ADS_SCOPE_SUBTREE = 2
 
Set objConnection = CreateObject("ADODB.Connection")
Set objCommand =   CreateObject("ADODB.Command")
objConnection.Provider = "ADsDSOObject"
objConnection.Open "Active Directory Provider"
Set objCommand.ActiveConnection = objConnection
 
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile("c:\OUUserlist.txt", 8, True)
 
objCommand.Properties("Page Size") = 1000
objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE 
 
objCommand.CommandText = _
    "SELECT Name,homedirectory,ProfilePath FROM 'LDAP://OU=test,DC=my,DC=com' WHERE objectCategory='user'"  
Set objRecordSet = objCommand.Execute
 
objRecordSet.MoveFirst
ObjTextfile.WriteLine(Date)
Objtextfile.WriteBlankLines(2)
Do Until objRecordSet.EOF
	ObjTextFile.writeline  objRecordSet.Fields("Name").Value & vbTab & objRecordSet.Fields("homedirectory").Value & vbTab & objRecordSet.Fields("ProfilePath").Value
    
    objRecordSet.MoveNext
Loop
Objtextfile.WriteBlankLines(2)
ObjTextfile.WriteLine("Total Records found:" & objRecordset.RecordCount)
Objtextfile.WriteBlankLines(2)
ObjTextFile.close

Open in new window

0
 
binarykukiCommented:
Replace LDAP:// value and text file location. And you are done. All if you need more attribute check this site out http://www.winzero.ca/Active-Directory-users.htm
On Error Resume Next
 
Const ADS_SCOPE_SUBTREE = 2
 
Set objConnection = CreateObject("ADODB.Connection")
Set objCommand =   CreateObject("ADODB.Command")
objConnection.Provider = "ADsDSOObject"
objConnection.Open "Active Directory Provider"
Set objCommand.ActiveConnection = objConnection
 
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile("c:\OUUserlist.txt", ForAppending, True)
 
objCommand.Properties("Page Size") = 1000
objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE 
 
objCommand.CommandText = _
    "SELECT Name FROM 'LDAP://dc=fabrikam,dc=com' WHERE objectCategory='user'"  
Set objRecordSet = objCommand.Execute
 
objRecordSet.MoveFirst
Do Until objRecordSet.EOF
	ObjTextFile.writeline  objRecordSet.Fields("SamAccountname").Value & "," & objRecordSet.Fields("homedirectory").Value & "," & objRecordSet.Fields("TerminalServicesProfilePath").Value
    objRecordSet.MoveNext
Loop
ObjTextFile.close

Open in new window

0
 
HaddionCommented:
You can use the csvde command line tool to do this.  Something like this should work.
CSVDE -d "OU=Users,DC=domain,dc=com" -r objectCategory=person -l "cn, profilePath, homeDirectory, mail"-f users.csv

Open in new window

0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
pmcguireAuthor Commented:
Forgive me for being dumb.  Both of these sound like they will work but i don't know how to implement them.  The first on i copied to notepad and modified for my domain and saved it as a vbs file.  Ran it and i get nothing.  The second copied it to notepad and saved as a bat file.  Still nothing.  I am running from a workstation.  Should i be on a domain controller?
0
 
HaddionCommented:
You need to adjust mine, so it has the correct OU information, then run it on the domain controller.
0
 
binarykukiCommented:
Has this solution worked for you?
0
 
pmcguireAuthor Commented:
The last bit of code read the ou i wanted but only wrote the summary to the text file.
02/05/2008
Total Records found:64

What needs to change to get it to list the info for each user in the OU?
On Error Resume Next
 
Const ADS_SCOPE_SUBTREE = 2
 
Set objConnection = CreateObject("ADODB.Connection")
Set objCommand =   CreateObject("ADODB.Command")
objConnection.Provider = "ADsDSOObject"
objConnection.Open "Active Directory Provider"
Set objCommand.ActiveConnection = objConnection
 
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile("c:\OUUserlist.txt", 8, True)
 
objCommand.Properties("Page Size") = 1000
objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE 
 
objCommand.CommandText = _
    "SELECT Name,homedirectory,ProfilePath FROM 'LDAP://OU=PENDING DELETION,DC=ftsad,DC=com' WHERE objectCategory='user'"  
Set objRecordSet = objCommand.Execute
 
objRecordSet.MoveFirst
ObjTextfile.WriteLine(Date)
Objtextfile.WriteBlankLines(2)
Do Until objRecordSet.EOF
	ObjTextFile.writeline  objRecordSet.Fields("Name").Value & vbTab & objRecordSet.Fields("homedirectory").Value & vbTab & objRecordSet.Fields("TerminalServicesProfilePath").Value
    
    objRecordSet.MoveNext
Loop
Objtextfile.WriteBlankLines(2)
ObjTextfile.WriteLine("Total Records found:" & objRecordset.RecordCount)
Objtextfile.WriteBlankLines(2)
ObjTextFile.close

Open in new window

0
 
pmcguireAuthor Commented:
I could not get the terminal services profile path to work.  everything else works when you take the terminal services profile out.  Thanks for your help.
0
All Courses

From novice to tech pro — start learning today.