Random PowerShell: Determine the longest job title in AD

This one’s kind of random, and a real edge case, but it shows how handy a bit of PowerShell knowledge is to quickly dig information out of Active Directory.

As part of a email signature redesign, we’d received a new HTML template from our web designers. Whilst testing out the template, I ran into an issue with text wrapping around if the user had a long job title. I needed to get back to the designers with an example of the longest job title we’d be likely to see in our environment.

Here’s how I discovered the longest job title for users in our domain:

Get-ADUser -Filter * -Properties title | Select-Object -ExpandProperty title -Unique | Sort-Object -Property Length | Select-Object -Last 1

I found that our longest job title was 60 characters long (to figure that out, wrap the entire command in parentheses and append ‘.ToCharArray().Count’)

As a side note, I normally never display code samples that use aliases on my blog, but here’s the command that I actually used. This is to illustrate that you don’t need to type out the long-winded version above just to perform a quick task.

get-aduser -filter * -properties title | select -expand title -unique | sort length | select -last 1