Description

This vbscript will allow you to list all the computers in a particular area of Active Directory (or the whole area) and also allows you to specify additional information that gets returned.

This script returns a CSV comma delimited file for easy viewing and reporting.

If you need additional information look in Active Directory at the attribute section of a computer. Then just reference the name of an attribute you want and modify the script to look for it.

This script has been tested with a Windows 7 machine.

Source Code

Dim objConnection, objCommand, objFile, strFile, strLDAP, strSelectAttr

'Set Variables
Const ADS_SCOPE_SUBTREE = 200
strFile = "adDetails.csv"
strLDAP = "OU=Computers,DC=example,DC=com"
strSelectAttr = "cn, operatingSystem, operatingSystemServicePack"

'Create CSV File
Set objFile = CreateObject("Scripting.FileSystemObject")   
Set strWrite = objFile.OpenTextFile(strFile, 2, True)
strWrite.WriteLine("Machine Name,Operating System,Service Pack")

Set objConnection = CreateObject("ADODB.Connection")
Set objCommand =   CreateObject("ADODB.Command")
objConnection.Provider = "ADsDSOObject"
objConnection.Open "Active Directory Provider"

Set objCommand.ActiveConnection = objConnection
objCommand.CommandText = _
    "Select " & strSelectAttr & " from 'LDAP://" & strLDAP & "' " _
        & "Where objectClass='computer'"  
objCommand.Properties("Page Size") = 1000
objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE 
Set objRecordSet = objCommand.Execute
objRecordSet.MoveFirst

Do Until objRecordSet.EOF
    strWrite.WriteLine( _
         objRecordSet.Fields("cn").Value & "," _
         & objRecordSet.Fields("operatingSystem").Value & "," _
         & objRecordSet.Fields("operatingSystemServicePack" _
    ).Value)

    objRecordSet.MoveNext
Loop

'close CSV file
strWrite.Close

MsgBox "Complete."

'cleanup
Set objConnection = Nothing
Set objCommand = Nothing
Set objFile = Nothing
Set strFile = Nothing
Set strLDAP = Nothing
Set strSelectAttr = Nothing

WScript.Quit
2 Spice ups

Nice. This is going to come in handy. Thank you!

Simply, you can use saved query in ADUC and export.

This didnt work for me in 2008 active directory services

Nice, I am planning on running a set of scripts to activate / deactivate computers based on OU. This goes a long way in that direction.

I am on a network with many domains. This works when I search in the domain I am connected. When I search in other domains the PC cant be found. Is there a easy modification that can be made to search the entire forest?