Link to home
Start Free TrialLog in
Avatar of usslindstrom
usslindstromFlag for Japan

asked on

List OUs via PowerShell / Nudge needed

Experts,

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 = $objItem.name

         Write-Host $OUName
    }

Open in new window

Avatar of Hendrik Wiese
Hendrik Wiese
Flag of South Africa image

ASKER CERTIFIED SOLUTION
Avatar of LindyS
LindyS
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of usslindstrom

ASKER

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.
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;
http://www.quest.com/powershell/activeroles-server.aspx
Understood.

Thank you all for your assistance here.  The information is very valuable.
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.