Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2014-12-09
7
Medium Priority
?
8,988 Views
Last Modified: 2014-12-19
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.
0
Comment
Question by:Risrollout
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
7 Comments
 
LVL 37

Expert Comment

by:Neil Russell
ID: 40489757
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

0
 
LVL 41

Expert Comment

by:footech
ID: 40489862
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

0
 

Author Comment

by:Risrollout
ID: 40490109
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?
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 41

Accepted Solution

by:
footech earned 1500 total points
ID: 40490144
I know it doesn't give the SID.  The Select statement tells it to display the DistinguishedName and Name properties (actually any property that ends with "name" from those available).  If you want the canonicalname (or any other property that isn't in the default set) that has to be specified with the -properties parameter of Get-ADOrganizationalUnit.
Get-ADOrganizationalUnit -Filter * -SearchBase "OU=someOU,dc=domain,dc=com" -Properties canonicalname | select canonicalname

Open in new window


BTW, Neilsr's should also give you the distinguishedNames.  I ran it and that's what I got.
1
 
LVL 41

Expert Comment

by:footech
ID: 40506086
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...
0
 

Author Comment

by:Risrollout
ID: 40506289
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
0
 
LVL 41

Expert Comment

by:footech
ID: 40509003
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.
0

Featured Post

Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Let's recap what we learned from yesterday's Skyport Systems webinar.
A bad practice commonly found during an account life cycle is to set its password to an initial, insecure password. The Password Reset Tool was developed to make the password reset process easier and more secure.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…
Suggested Courses

618 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