Thursday, March 30, 2017

Complex properties from Get-ADUser

If you have every tried to export a list of AD Users along with their ProxyAddresses into a CSV file using Get-ADUser, you'll understand the pain of using powershell sometimes. Here's a quick example:


"DistinguishedName","Enabled","GivenName","Name","ObjectClass","ObjectGUID","proxyaddresses","SamAccountName","SID","Surname","UserPrincipalName"
"CN=jondoe,DC=contoso,DC=com","True","Jon","jondoe","user", "00000000-000-0000000", "Microsoft.ActiveDirectory.Management.ADPropertyValueCollection", "jondoe","S-1-5-21-00000-00000-0000000-00000",,"jondoe@contoso.com"


The problem is while most attributes like DistinguishedName, Enabled, GivenName, etc. are simple text value, ProxyAddresses shows up as an ADPropertyValueCollection. Showing me its type does not help.

Here's a quick and dirty way I use to address this issue:
Get-ADUser jondoe -Property ProxyAddresses | select Name,UserPrincipalName,@{n="ProxyAddresses";e={$_.ProxyAddresses -join "|"}}
You can change the "|" character to anything you like, but you get the idea. It works just like magic!