One of my favourite cmdlets in PowerShell is export-csv. It has one major flaw though… It doesn’t handle multi-valued attributes. For those of us regularly working with Active Directory and Exchange, this can be very frustrating. I’ve used a variety of different methods for solving this problem, but recently came across some code that I adapted to work like a function (I will reference the original code here once I find it again).

Before Before Using Function
After After Using Function

Magic!

Example

$object | Convert-MultiValuedStringsToString | export-csv $path

Script

<#
    .SYNOPSIS
       Enumerates all properties in an object and converts multi valued properties to a string separated by the specified separator

    .DESCRIPTION
       This function enumerates all properties in an object and converts multi valued properties to a string separated by the specified separator
 
    .PARAMETER Seperator
            Required?                    false
            Position?                    0
            Accept pipeline input?       false
            Parameter set name           All
            Aliases                      None
            Dynamic?                     false

         
    .PARAMETER Object
            Required?                    true
            Position?                    1
            Accept pipeline input?       true (ByPipeline, ByValue, ByPropertyName)
            Parameter set name           All
            Aliases                      None
            Dynamic?                     true
 
    .EXAMPLE
       $object | Convert-MultiValuedStringsToString | export-csv $path
 
       Converts all multi valued properties in the object $object to a string separated by a new line

    .EXAMPLE
       $object | Convert-MultiValuedStringsToString ";" | export-csv $path
 
       Converts all multi valued properties in the object $object to a string separated by ;

    .FUNCTIONALITY
       This function enumerates all properties in an object and converts multi valued properties to a string separated by the specified separator

    .NOTES 
        Author:  Scott Breen
        Email:   scott.breen@outlook.com
        Date:    10/06/2014
#>
function Convert-MultiValuedStringsToString {

    param
    (
        [Parameter(Mandatory = $false, 
                    ValueFromPipeline=$false,
                    ValueFromPipelinebyPropertyName=$false,
                    ValueFromRemainingArguments=$false,
                    Position=0
        )]
        [ValidateNotNullOrEmpty()]
        [String] $Seperator = "`n",
        [Parameter(Mandatory = $true, 
                    ValueFromPipeline=$true,
                    ValueFromPipelinebyPropertyName=$true,
                    ValueFromRemainingArguments=$false,
                    Position=1
        )]
        [ValidateNotNullOrEmpty()]
        [object] $object #Message,
        
    )
 
    Process {
        $results= $object |
	    ForEach-Object {
		    $properties = New-Object PSObject    
		    $_.PSObject.Properties | 
			    ForEach-Object {
				    $propertyName = $_.Name
				    $propertyValue = $_.Value
				    If ($propertyValue -NE $NULL) { 
					    $values = @()
					    ForEach ($value In $propertyValue) {
						    $values += $value.ToString()
					    }
					    Add-Member -inputObject $properties NoteProperty -name $propertyName -value "$([String]::Join($Seperator,$values))"
				    } Else { 
					    Add-Member -inputObject $properties NoteProperty -name $propertyName -value $NULL
				    }
			    }
		    $properties
	    }
    
        return $results
	}
}