PowerShell Script uses Split() Method to display Active Directory Users’ Organizational Unit and other Properties.

A colleague was trying to generate a report of Active Directory Users, displaying their Organizational Unit and PasswordNeverExpires property status. Normally this should be very simple, but they ran into a small snag.

The Active Directory module at the time of this writing which is PowerShell 4.0 does not have a direct Organizational Unit property unlike Exchange Management Shell. So I came up with a one line script to extract the Organizational Unit from a known property and display the report as needed:

PS C:\> Get-ADUser -Filter "ObjectClass -eq 'user' -and Enabled -eq 'true'" -Properties PasswordNeverExpires | Select-Object Name, @{Name='OU';Expression={($_.DistinguishedName.Split(',', 2))[1]}}, PasswordNeverExpires | Sort-Object -Property PasswordNeverExpires

First, the Get-ADUser cmdlet retrieves all users, filtering by the objectclass equals user property and enabled status set to true. We would like to display the PasswordNeverExpires property. Since the current Active Directory module does not have an Organizational Unit property, I decided to use the Split() method of the DistinguishedName property to extract the Organizational Unit substring of the same object . The split method takes a string and splits it into one or more elements or substrings, determined by the split delimiter or separator and number of elements or fields or substrings.The result is sorted by the PasswordNeverExpires property.

A section of the displayed result is shown below. The result could also be piped to a CSV file using the Export-CSV cmdlet. I hope this helps.


This entry was posted in Active Directory, Powershell 4.0, Script and tagged , , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s