List OUs via PowerShell / Nudge needed

Posted on 2011-10-05
Last Modified: 2012-05-12

I have the following script (posted in the code block below), and am attempting to have PowerShell list OUs...  But I haven't been successful.

Would anybody happen to be able to point me in the right direction?
$strFilter = "(&(objectCategory='organizationalUnit'))"

$objDomain = New-Object System.DirectoryServices.DirectoryEntry

$objSearcher = New-Object System.DirectoryServices.DirectorySearcher
$objSearcher.SearchRoot = $objDomain
$objSearcher.PageSize = 1000
$objSearcher.Filter = $strFilter
$objSearcher.SearchScope = "Subtree"

$colProplist = "name"
foreach ($i in $colPropList){$objSearcher.PropertiesToLoad.Add($i)}

$colResults = $objSearcher.FindAll()

foreach ($objResult in $colResults)
    {$objItem = $objResult.Properties
        $OUName = $

         Write-Host $OUName

Open in new window

Question by:usslindstrom
    LVL 20

    Expert Comment

    by:Hendrik Wiese
    LVL 3

    Accepted Solution

    If you have the Quest Active Roles snapin, you can run this simple command.

    Get-QADObject -type organizationalunit -searchroot 'Domain/OU' | Select Name,ParentContainer | Export-CSV OU_Listing.csv -notype
    LVL 20

    Assisted Solution

    by:Hendrik Wiese
    This will export the data to C:\temp\ngtest.csv

    # Initialize array with two fields:
    # Distribution group, Members
    $totalObj = @()
    # Retrieve all DGs
    $temp = Get-DistributionGroup -ResultSize Unlimited |  
           # Loop through all distribution groups
           ForEach-Object {           
                 # Add the members of the DG to an array
                 [array]$mem = Get-DistributionGroupMember -id $_      
                 # Loop through the DG and assign each member name to the variable $member
                 for ($i = 0; $i -lt $mem.Count; $i++) {
                        $member = $mem[$i].name
                        # Create instance of object of type .NET
                        $obj = New-Object System.Object
                        # Add the name of the DG to the object
                        $obj | Add-Member -MemberType NoteProperty -Value $_.Name -Name 'Distribution Group' -Force
                        # Add the member name to the object
                        $obj | Add-Member -MemberType NoteProperty -Value $member -Name 'Members' -Force -PassThru
                        # Add the object to the array
                        $totalObj += $obj
    # Pipe output to .csv file
    $totalObj | Export-Csv -Encoding 'Unicode' c:\temp\ngtest.csv
    The output is written like this to the csv. file:
    "Distribution Group",Members
    "All Users","Jon-Alfred Smith"
    "All Users","Julie Smith"
    "Windows SBS Administrators","Standard User with administration links"
    "Windows SBS Administrators","Jon-Alfred Smith"
    If you just want to have the name of the DG once, change this line: Only add the name the first time, when the counter is zero:
    # Add the name of the DG to the object
    if ($i -eq 0) {
    $obj | Add-Member -MemberType NoteProperty -Value $_.Name -Name 'Distribution Group' -Force

    Open in new window

    LVL 11

    Assisted Solution

    Not necessarily the fastest and optimized way:
    Import-Module ActiveDirectory
    cd "dc=your,dc=domain"
    # The easiest way (very UNOPTIMIZED). It'll browse through whole Directory, and then will check the objectClass property:
    Get-ChildItem -Recurse | Where-Object {$_.objectClass -eq "organizationalUnit"} | Select-Object Name, distinguishedName | Export-Csv c:\out.txt

    Open in new window

    LVL 5

    Author Comment

    Thanks for the suggestions so far guys.

    HendrikWiese, much appreciated on that code.  Group Membership was going to be my next task.  For this one though, I'm needing to just work with the OUs in AD.

    LindyS & marek1712, thank you very much for the versions of code you guys provided.  It's exactly what I was needing.  But I have two questions if you wouldn't mind.

    1.  I'd like to keep this in native PowerShell, without having to load any additional PS Modules.  These both get me what I need, but my goal is to be able to run this on units that don't have things like Qwest AD PowerShell extensions, etc.

    2.  Both versions of scripts that you guys gave me are recursive.  They work great, but would you happen to know how I could just have it spew out a list of OUs at the root of where I'm targeting, and not Sub-OUs?

    Thanks for everything so far guys, I really do appreciate the help.
    LVL 3

    Expert Comment

    The easiest way to do that, if you don't want to use any snapins, would be simply to use ADUC.
    Right click on the parent container and choose export list. It will provide you with a list of all OU's under that in either a txt or csv format. You can add or remove columns to provide what details you need.

    If you decide to use Active Roles, it is a free download, and you don't need the server to use the shell.
    The download link is;
    LVL 5

    Author Closing Comment


    Thank you all for your assistance here.  The information is very valuable.
    LVL 11

    Expert Comment

    usslindstrom - I prefer not to use external PowerShell snap-ins too.
    ActiveDirectory module is a native one (from Microsoft) and can be found either on domain controllers or workstations with RSAT installed. Nothing else is required.
    If you want to get rid of SubOUs - the remove the "-Recurse" switch. You'll have to modify the second step though.

    Featured Post

    Find Ransomware Secrets With All-Source Analysis

    Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

    Join & Write a Comment

    Are you one of those front-line IT Service Desk staff fielding calls, replying to emails, all-the-while working to resolve end-user technological nightmares? I am! That's why I have put together this brief overview of tools and techniques I use in o…
    In this previous article (, we made basic license assignments to users in O365. When I say basic, the method is the simplest way …
    Need more eyes on your posted question? Go ahead and follow the quick steps in this video to learn how to Request Attention to your question. *Log into your Experts Exchange account *Find the question you want to Request Attention for *Go to the e…
    In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…

    729 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

    19 Experts available now in Live!

    Get 1:1 Help Now