Exporting Multiline fields to CSV

I am trying to make a cmdlet reference as a cheat sheet while I study for the MCSA. I would like a spreadsheet that contains the cmdlet name, the module, the synopsis and the description. The problem comes in on the description side. I can use the code below to loop through my commands and build an array of name module and synopsis.

$commands_array = @()
$commands = 'Enter-PSSession','Get-Command'

foreach ($command in $commands)

{

$item = New-Object PSObject
$command_help = Get-Help $command
$command_description = $command_help | select -ExpandProperty description
$item | Add-Member -NotePropertyName Name -NotePropertyValue $command_help.Name
$item | Add-Member -NotePropertyName ModuleName -NotePropertyValue $command_help.ModuleName
$item | Add-Member -NotePropertyName Synopsis -NotePropertyValue $command_help.Synopsis

$commands_array += $item
}


$commands_array | Export-Csv -Path D:\Profile\Desktop\cmdlets.csv -NoTypeInformation

But when I throw in the description line after the synopsis

$commands_array = @()
$commands = 'Enter-PSSession','Get-Command'

foreach ($command in $commands) 

{

$item = New-Object PSObject
$command_help = Get-Help $command
$command_description = $command_help | select -ExpandProperty description
$item | Add-Member -NotePropertyName Name -NotePropertyValue $command_help.Name
$item | Add-Member -NotePropertyName ModuleName -NotePropertyValue $command_help.ModuleName
$item | Add-Member -NotePropertyName Synopsis -NotePropertyValue $command_help.Synopsis
$item | Add-Member -NotePropertyName Description -NotePropertyValue $command_description

$commands_array += $item
}

$commands_array | Export-Csv -Path D:\Profile\Desktop\cmdlets.csv -NoTypeInformation

The CSV file has System.Object as the value for the description field. I think the issue is that for some reason the description field is an array of text values. I cannot figure out for the life of me how to export the full description to the description field in the CSV. Ideally with the same multi-line formatting with all the pretty line breaks and such, but I would take a straight dump at this point.

Any guidance would be greatly appreciated!

Thank you much!!

try this:

$commands_array = @()
$commands = 'Enter-PSSession','Get-Command'
foreach ($command in $commands) 
{
 $item = New-Object PSObject
 $command_help = Get-Help $command
 $command_description = $command_help | select -ExpandProperty description
 $item | Add-Member -NotePropertyName Name -NotePropertyValue $command_help.Name
 $item | Add-Member -NotePropertyName ModuleName -NotePropertyValue $command_help.ModuleName
 $item | Add-Member -NotePropertyName Synopsis -NotePropertyValue $command_help.Synopsis
 $item | Add-Member -NotePropertyName Description -NotePropertyValue ($command_description | Out-String).trim()
 
$commands_array += $item
}

$commands_array | Export-Csv -Path "D:\Profile\Desktop\cmdlets.csv" -NoTypeInformation

Further Info:https://learn-powershell.net/2014/01/24/avoiding-system-object-or-similar-output-when-using-export-csv/

The description is an object array with multiple string objects and not one big single string.

You could do:

$command_description = $command_help.description | Out-String

Which will give you a “dump” or single string from those string objects.
Haven’t tested though how e.g. Excel would interpret that output after an export but you could give it a try.

btw, you could also replace all that New-Object code and Add-Member code with a hashtable, e.g: “[pscustomobject][ordered] @{}”

 

for more info, check out https://mcpmag.com/articles/2012/12/11/pshell-order.aspx

@Tom Yates

Thank you greatly! Worked like a champ. I knew it was something easy, just didn’t know exactly what.

I’m still not 100% sure as to why that works, but knowing how to fix it is good enough for me today.

Thank you all so much for your help!!