Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 70
  • Last Modified:

How to get a list of Organisational Units from AD with only 4 characters in their name

Hi,
Wondering if anyone could help me with a command set that would output all the canonical names of OU's that have exactly  4 characters in their name?
0
Hal-itosis
Asked:
Hal-itosis
  • 3
  • 2
1 Solution
 
nashiookaCommented:
Using PowerShelll with the AD module loaded so:

Import-Module ActiveDirectory
Get-ADSite | ?{$_.Name.Length -eq 4}

Note: you must have at least the RSAT tools loaded to get access to the module.

Hope that helps.

Steven
0
 
nashiookaCommented:
Oh if you want just the names you can add | %{$_.Name} to the end of that last command.
0
 
Guy LidbetterCommented:
Hi Hal-itosis ... (Nice Name)

Try this:

$OUs = Get-ADOrganizationalUnit -Properties * | select *
foreach ($OU in $OUs) {IF ($OU.Name.Length -eq 4) {Write-Host $OU.DistinguishedName}}

Open in new window


OR

 Get-ADOrganizationalUnit -Filter * | Where {$_.Name.Length -eq 4} | Select DistinguishedName

Open in new window


Regards

Guy
0
New Tabletop Appliances Blow Competitors Away!

WatchGuard’s new T15, T35 and T55 tabletop UTMs provide the highest-performing security inspection in their class, allowing users at small offices, home offices and distributed enterprises to experience blazing-fast Internet speeds without sacrificing enterprise-grade security.

 
nashiookaCommented:
Sorry I gave you sites before, fingurs quicker than the mind.  Building off Guy's comment:

Get-ADOrganizationalUnit -Filter * | Where {$_.Name.Length -eq 4} | %{$_.Name}

I'm pretty sure name and CN are always the same.  .
0
 
Hal-itosisAuthor Commented:
Excellent, thanks for the idea's.

What met my criteria exactly was:
Get-ADOrganizationalUnit -Filter * | Where {$_.Name.Length -eq 4} | Select DistinguishedName

I tried the variations with Get-ADOrganizationalUnit cmmdlet but I'm no good with the "Where {$_.Name.Length -eq 4" stuff.

Thanks a lot to you both. Guy Lidbetter for the exact answer.
0
 
Guy LidbetterCommented:
Just though providing distinguishedname (CN) rather than just name would be more useful as you can see where the actual OU is located.
0

Featured Post

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now