Saturday , December 14 2019
Home - Development - Easy Find OU in a DistinguishedName with PowerShell

Easy Find OU in a DistinguishedName with PowerShell

There are a couple of ways to find the OU of an Object, some are long-winded ways splitting and counting characters.  The best way I’ve come up with for a quick and case insensitive check:

A better example of the use in code:

The Case Culture fixes up the compare of string being a different case of “OU=”, otherwise it may fail depending on code use.  Notice the above example having the OU being in lower case for “ou=Corp” but it still matches “OU=Corp”.

This can also be used on a collection object adding a new column called the “ParentOU”.  The following example show how to get a build $users with a list of users from the Domain but only shows two columns:

We can modify the collection to have an additional column with the following:

This results in the following collection format table:

Notice, the user objects in the Users Folder in Active Directory.  It’s a folder and not an Organisation Until so the ParentOU is blank.

To add the column to the collection object:

Check Also

How to invoke/test Office 365 (Exchange Online) cmdlets via Remote Powershell

I’ve been working on some PowerShell scripts for use with Office 365 and needed to invoke/test …

Leave a Reply

Your email address will not be published. Required fields are marked *