Matching on DistinguishedName in Active Directory

In Active Directory, I need to find all OUs that are “AdminUsers”
One approach is to look at all users’ DistinguishedName. Is there a better approach?
Looking at all user’s DistinguishedNames - I would look at all the users to see if any OU in DistinguishedName matches “AdminUsers”
When found I need to see the DistinguishedName on the console or export it to a CSV file.
But there would be a lot of duplication using this approach.
Any OU in DistinguishedName may contain “AdminUsers”
Like this:
I get no errors when I run this but I stopped it after it ran for a 60 seconds and nothing happened. I want to see what you think about this:

$users = get-aduser -Filter *
$String = DisginguishedName
If ($string -match “adminusers”) {$users | Select-Object DistinguishedName, Name | ft}

$users | Select-Object DistinguishedName, Name, @{n=“ParentOU”;e={$($.DistinguishedName.Substring($($.DistinguishedName).IndexOf(‘OU=’,[System.StringComparison]::CurrentCultureIgnoreCase)))}} | ft

Is there a better way? And is there anything wrong with this script?
Thank you very much.

If you want the OU, why not use Get-ADOrganizationalUnit

Get-ADOrganizationalUnit -filter 'Name -like "*admin*"' | Select-Object -ExpandProperty distinguishedName
1 Like

Matt-Bloomfield, Of course! I thought there might be a better approach. I will expand from this. Thanks.