Avatar of Robert
Robert
Flag for United States of America asked on

PowerShell Script to list child OUs with in a given Parent OUs.

To the point. I need a PS script that will list the child OUs with in a given parent OU. Example of the OU structure.

Parent OU  -  OU=CHI,dc=NA,dc=mydomain,dc=net


I have tried ADfind and other scripts with no success.
PowershellActive Directory

Avatar of undefined
Last Comment
footech

8/22/2022 - Mon
Neil Russell

I use the following technique.

$sourceOU = "OU=CHI,dc=NA,dc=mydomain,dc=net"
[array] $OUs = @() 
$OUs = dsquery * $sourceOU -Filter "(objectCategory=organizationalUnit)" -limit 0

Open in new window

footech

You can do something like this.  I've included a custom Sort which makes things a little easier to view.
Get-ADOrganizationalUnit -Filter * -SearchBase "OU=someOU,dc=domain,dc=com" | Sort {-join ($_.distinguishedname[($_.distinguishedname.length-1)..0])} | Select *name | ft -auto

Open in new window

Robert

ASKER
Neilsr,
Your script only out puts random numbers for me.


Footech,
Yours gives me the SID number of the OU not the Canonical Name.  Can you fix yours where it will show the CanonicalName?
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
ASKER CERTIFIED SOLUTION
footech

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
footech

I'm curious why the answer was rated a "B".  It appears that the code does exactly what you asked for.  How could I have made it an "A" answer?

You've been a member here for quite some time so I would assume you're familiar with the grading guidelines...
Robert

ASKER
Yes I have been a member for some time and try to give as much credit as I can to the people nice enough to answer. I gave you a B because you did not give me a working script. Most of it was there but not all. With out "Import-Module -Name ActiveDirectory" your script did not work for me. I hope this helps you and others.

This is what I finished with.

Import-Module -Name ActiveDirectory
Get-ADOrganizationalUnit -Filter * -SearchBase "OU=CHI,dc=NA,dc=mydomain,dc=net"  -Properties CanonicalName | Select-Object -Property CanonicalName | Export-CSV -Path c:\YourSite.csv
footech

I appreciate the response.  It's not a large issue so I don't feel the need to go on beyond this post.

I find it excessive to lower a grade because I didn't provide Import-Module -Name ActiveDirectory
Using PS 3.0 and newer the module is loaded automatically if it's available.  If you had an issue running the command I could have easily answered that, but if you don't ask the question there's little point in guessing what your environment is and providing variations for whatever you may have.
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.