VBS script to find computer accounts in a specific OU

Posted on 2011-05-03
Last Modified: 2012-05-11
I currently have a script that adds a domain user as a local administrator of any computer we specify located on our domain. What I would like to have as well is a script that will cycle through a OU in AD and get a list of all the computers in that OU and then I can run my script to add a user to all the computers in the OU. LOL ok say that three times fast.

Can anyone help me with this VBS script?
Question by:cbielich
    LVL 15

    Expert Comment

    Please refer to the link below. This provides a couple of good vbsripts which should hopefully provide the required results.
    LVL 12

    Accepted Solution

    Give this a try.  Modify the path to your OU on line: 11
    Const ADS_SCOPE_SUBTREE = 2   
    Const ForAppending = 8   
    Set fso = CreateObject("Scripting.FileSystemObject")   
    Set outFile = fso.OpenTextFile("Computers.txt", ForAppending, True)   
    Set cn = CreateObject("ADODB.Connection")   
    cn.Provider = "ADsDSOObject"  
    cn.Open "Active Directory Provider"  
    Set cmd = CreateObject("ADODB.Command")   
    Set cmd.ActiveConnection = cn   
    ou = "OU=3rdOU,OU=2ndOU,OU=TopOU,DC=YourDomain,DC=com"  
    cmd.CommandText = "SELECT name " & _   
                      "FROM 'LDAP://" & ou & "' " & _   
                      "WHERE objectClass='computer' " & _   
                      "ORDER BY name"     
    cmd.Properties("Page Size") = 1000   
    cmd.Properties("Searchscope") = ADS_SCOPE_SUBTREE   
    Set rs = cmd.Execute   
    Do Until rs.EOF   
      WScript.Echo rs(0)   
      outFile.WriteLine rs(0)   
    Set outFile = Nothing  
    Set fso = Nothing

    Open in new window

    LVL 12

    Expert Comment

    Here's another method:
    '##  Change example strOU & strOutputFile variables to suit
    strOU = "OU=Computers,OU=London,DC=lon-dom,DC=com"
    strOutputFile = "c:\ComputerList.txt"
    Set colItems = GetObject("LDAP://" & strOU)
    colItems.Filter = Array("Computer")
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set ts  = fso.OpenTextFile(strOutputFile, 2, True)
    For Each objItem In colItems
         ts.WriteLine objItem.CN
    MsgBox "Done!"

    Open in new window

    Hope this helps,
    LVL 1

    Author Comment

    Can I get the output to loop and spit out a variable of the computer name so I can just incorporate my code for adding the account to the computer?
    LVL 12

    Expert Comment

    You could add your code as a function and call the function where my script echos the computer name, passing the current computer name.
    the computer would have to be on, and you would have to add error checking for that.

    Featured Post

    What Should I Do With This Threat Intelligence?

    Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

    Join & Write a Comment

    This is an addendum to the following article: Acitve Directory based Outlook Signature ( The script is fine, and works in normal client-server domains…
    If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
    This video discusses moving either the default database or any database to a new volume.
    In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor ( If you're interested in additional methods for monitoring bandwidt…

    754 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now