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

Posted on 2015-02-20
Last Modified: 2015-02-20
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?
Question by:Hal-itosis
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
  • 3
  • 2
LVL 10

Expert Comment

ID: 40620895
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.

LVL 10

Expert Comment

ID: 40620896
Oh if you want just the names you can add | %{$_.Name} to the end of that last command.
LVL 13

Accepted Solution

Guy Lidbetter earned 500 total points
ID: 40620899
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


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

Open in new window


Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

LVL 10

Expert Comment

ID: 40620906
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.  .

Author Closing Comment

ID: 40620916
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.
LVL 13

Expert Comment

by:Guy Lidbetter
ID: 40620917
Just though providing distinguishedname (CN) rather than just name would be more useful as you can see where the actual OU is located.

Featured Post

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

This script can help you clean up your user profile database by comparing profiles to Active Directory users in a particular OU, and removing the profiles that don't match.
A brief introduction to what I consider to be the best editor for PowerShell.
This Micro Tutorial will give you a basic overview of Windows DVD Burner through its features and interface. This will be demonstrated using Windows 7 operating system.
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…

749 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