Outputting collections from AD

Have you ever tried anything like this:

 

Get-ADUser -Filter * -Properties * |
Select Name, LastLogondate, PasswordNeverExpires, Enabled,
@{N=’OU’; E={($($_.distinguishedName) -split “,”,2)[1] }},
Memberof |
Export-csv -Path users.csv -NoTypeInformation

 

You want some basic information about the users plus their group membership

What you get is this

Name                 : Fred Friday
LastLogondate        :
PasswordNeverExpires : False
Enabled              : True
OU                   : OU=Mailboxes,DC=Manticore,DC=org
Memberof             : Microsoft.ActiveDirectory.Management.ADPropertyValueCollection

The data is OK apart from the group membership where you get the name of the object.

All you need is a little trick with sub expressions

Get-ADUser -Filter * -Properties * |
Select Name, LastLogondate, PasswordNeverExpires, Enabled,
@{N=’OU’; E={($($_.distinguishedName) -split “,”,2)[1] }},
@{N=’Groups’; E={$($_.Memberof)}} |
Export-csv -Path users.csv –NoTypeInformation

and then the data comes through

Name                 : Fred Friday
LastLogondate        :
PasswordNeverExpires : False
Enabled              : True
OU                   : OU=Mailboxes,DC=Manticore,DC=org
Groups               : CN=Wednesdaylist,OU=DistributionGroups,DC=Manticore,DC=org

Enjoy

Advertisements
This entry was posted in PowerShell and Active Directory. Bookmark the permalink.

2 Responses to Outputting collections from AD

  1. effe says:

    Thank you so much! Been searching for 3 hours for a simple solution like this.. Thanks!

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 )

Google+ photo

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

Connecting to %s