Option Explicit
Dim objCommand, objConnection, strBase, strFilter, strAttributes, objUser
Dim strQuery, objRecordset, strdistinguishedName, strTSPath, strCN
Set objCommand = CreateObject("ADODB.Command")
Set objConnection = CreateObject("ADODB.Connection")
objConnection.Provider = "ADsDSOObject"
objConnection.Open "Active Directory Provider"
objCommand.ActiveConnection = objConnection
'...set the base DN
strBase = "<
LDAP://DC=MYCO,DC=COM>"
strFilter = "(&(objectCategory=person)(objectClass=user))"
strAttributes = "sAMAccountName,cn,distinguishedName"
strQuery = strBase & ";" & strFilter & ";" & strAttributes & ";subtree"
objCommand.CommandText = strQuery
objCommand.Properties("Page Size") = 100
objCommand.Properties("Timeout") = 30
objCommand.Properties("Cache Results") = False
Set objRecordSet = objCommand.Execute
Do Until objRecordSet.EOF
strdistinguishedName = objRecordSet.Fields("distinguishedName").Value
Set objUser = GetObject("LDAP://" & strdistinguishedName)
On error resume next
Wscript.Echo objUser.cn & "," & objUser.sAMAccountName _
& "," & objUser.TerminalServicesProfilePath & "," & objUser.profilePath _
& "," & objUser.homeDirectory & "," &objUser.ScriptPath
objRecordSet.MoveNext
Loop
To use the script to write out to a CSV file, simply use the following command line (assumes you have saved the script as profiledump.csv):