This is closer to what you want and is easier to manage.
You can do the header by looping the fields also. I will leave that to you.
VBScript Code
Double-click the code block to select all.
set objFSO = CreateObject("scripting.filesystemobject")
set objFile = objFSO.createtextfile(".\Review" & "_" & Year(Now()) & Month(Now()) & Day(Now()) & ".txt" , 2, True)
strDNSDomain = GetObject("LDAP://RootDSE").Get("defaultNamingContext")
strBase = "<LDAP://" & strDNSDomain & ">"
strFilter = "(&(objectCategory=person)(objectClass=user)(!userAccountControl:1.2.840.113556.1.4.803:=2))"
strAttributes = "samaccountname,lastLogonTimeStamp,useraccountcontrol,pwdLastset,employeetype,company,mail,sn,givenName,modifyTimeStamp,initials,manager,title,department,employeeid,l,streetAddress,postalCode,postofficeBox,st,manager,co,TelephoneNumber,physicalDeliveryOfficeName,mobile"
adoCommand.CommandText = strBase & ";" & strFilter & ";" & strAttributes & ";subtree"
Set adoConnection = CreateObject("ADODB.Connection")
adoConnection.Provider = "ADsDSOObject"
adoConnection.Open "Active Directory Provider"
Set adoCommand = CreateObject("ADODB.Command")
adoCommand.ActiveConnection = adoConnection
adoCommand.Properties("Page Size") = 500
Set adoRecordset = adoCommand.Execute
Do Until adoRecordset.EOF
With adoRecordset
WriteField .Fields("givenName").Value
WriteField .Fields("initials").Value
WriteField .Fields("sn").Value
WriteField .Fields("samaccountname").Value
WriteField .Fields("mail").Value
WriteField .Fields("employeeID").Value
WriteField .Fields("streetAddress").Value
WriteField .Fields("l").Value
WriteField .Fields("st").Value
WriteField .Fields("postalCode").Value
WriteField .Fields("co").Value
WriteField .Fields("physicalDeliveryOfficeName")
WriteField .Fields("TelephoneNumber").Value
WriteField .Fields("mobile").Value
WriteField .Fields("company").Value
WriteField .Fields("title").Value
WriteField .Fields("department").Value
WriteField .Fields("manager").Value
WriteField .Fields("postOfficeBox").Value
intUAC=.Fields("userAccountControl").Value
If intUAC AND 2 Then 'ADS_UF_ACCOUNTDISABLE = 2
strStatus="DISABLED"
Else
strStatus="ENABLED"
End If
WriteField strStatus
WriteField ConvertDate(.Fields("pwdLastSet").Value)
' specil handling for last column just close quotes
strDate = ConvertDate(.Fields("lastLogonTimeStamp").Value)
objFile.WriteLine """" & strDate & """"
.MoveNext
Loop
Wscript.echo "Script Complete"
objFile.Close
Function WriteField( strValue )
If IsNull(strValue) Then
objFile.Write "," 'just use comma for empty field
Else
objFile.Write strValue & """," ' close quote and add comma.
End If
End Function
Function ConvertDate( adDate )
If (TypeName(adDate) = "Object") Then
Set objDate = .Fields("pwdLastSet").Value
dtmPwdLastSet = Integer8Date(objDate, lngBias)
Else
dtmPwdLastSet = #1/1/1601#
End If
End Function