Dump computer accounts into a text file

Hi Experts.  I know this is easy but I have tried to do it on my own and haven't been successful.  I suck at scripting so please be patient.

I need to dump the computer accounts in my domain to a text file.  I am not sure how to write or run this.  Thank you for your help.
Who is Participating?
Install an ldapsearch utility if you don't already have one.  You can get a free one from cygwin.com.  Then you can simply query AD using ldapsearch like so:

ldapsearch -H ldap://server -D username -x -w password -b ou=Workstations,dc=mycompany,dc=com "(objectclass=computer)" "cn" > computers.txt

Obviously, you'll have to look up some of those query parameters from your own AD server.  Configurations vary company to company.
samiam41Author Commented:
Thanks!  That gives me some place to start from.  I will let you know how it goes.
Lee W, MVPTechnology and Business Process AdvisorCommented:
Or, on a DC, type:


Of course, that won't just get the workstations, that will get every computer in the domain.

However, you can still use DSQUERY to get computers in a specific OU if you want to.

More info on DSQUERY
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

samiam41Author Commented:
leew > I don't have access to the DC from where I am.  

I am still working on this and will update later this morning.
samiam41Author Commented:
On the dsquery, maybe I am missing something (wouldn't doubt it) but this looks like it will provide OU's in the domain.

dsquery ou [{<StartNode> | forestroot | domainroot}] [-o {dn | rdn}][-scope {subtree | onelevel | base}][-name <Name] [-desc <Description>] [{-s <Server> | -d <Domain>}] [-u <UserName>] [-p {<Password> | *}] [-q] [-r] [-gc] [-limit <NumberOfObjects>] [{-uc | -uco | -uci}]
samiam41Author Commented:
I found my old post that had the script I needed.

Dim counter
'Create New excel workbook
Set Excel = CreateObject("Excel.Application")
Excel.Visible = True
counter =1 'init to first cell
Function DoRecursive(strObjectDN)
  'Retrieve a list of OU's for this level
 Set oneLevelOU= GetObject("LDAP://" & strObjectDN)
 For Each OU in oneLevelOU
'if ou is computers then add to
 If InStr(1,ou.name,"Computers")>0 Then 
Set objOU = GetObject("LDAP://" &  OU.name & "," & strObjectDN)
objOU.Filter = Array("Computer")
For Each objComputer in objOU
       Excel.Cells(counter,1).Value = objComputer.CN
           Excel.Cells(counter,2).Value = "LDAP://" &  OU.name & "," & strObjectDN
       counter =counter +1
 End If
    DoRecursive( OU.name & "," & strObjectDN)
End Function

Open in new window

samiam41Author Commented:
Thanks for your help.  I never used your suggestion put it gave me an additional resource to try and I will accept your answer.  Take care!

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.