PowerShell : Exporting multi-valued attribute via Export-Csv cmdlet

The attributes that are multi-valued are hard to export to a CSV via the Export-Csv cmdlet as the exported value just shows the string type in Excel/Notepad.

For instance, take a look below when I try to export the proxyAddresses attribute values in PowerShell console and to a CSV later.

image

image

I found out that you can using the join function i.e @{Name=’proxyAddresses’;Expression={[string]::join(";", ($_.proxyAddresses))}} can export the multiple values from a multi-valued attribute to a CSV accordingly.

So, this is how it would look for the query I ran above.

Get-QADUser test.user1 -IncludeAllProperties | select name,@{Name='proxyaddresses';Expression={[string]::join(";", ($_.proxyaddresses))}} | Export-Csv .testUser1.csv

 

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, “Courier New”, courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

To accomplish the export of all values in a spreadsheet/csv.

image

This should come handy also when you are trying to retrieve the ‘memberof’ attribute of users and trying to export all groups that a user is part of to a CSV. Just replace the attribute you are after in the join function above.

tags:
posted in PowerShell by Rick

Follow comments via the RSS Feed | Leave a comment | Trackback URL

3 Comments to "PowerShell : Exporting multi-valued attribute via Export-Csv cmdlet"

  1. Steve wrote:

    So, I’ve just run a report that used this expression, but is there a way to have it generate the display names, rather than the DN?

    Thanks Rick!!

  2. Rick wrote:

    Hi Steve, Before the first ‘pipe’, try this instead.

    select displayname,@{Name=’proxyaddresses’;Expression={[string]::join(“;”, ($_.proxyaddresses))}}

  3. Barry wrote:

    Hi Shariq,

    Thank you for this very valuable tip. Brilliant.

Leave Your Comment

 
(c) 2008 - 2012 Shariq Sheikh. All Rights Reserved.