Exporting Multiline fields to CSV

Welcome Forums General PowerShell Q&A Exporting Multiline fields to CSV

This topic contains 4 replies, has 4 voices, and was last updated by

 
Participant
3 months, 2 weeks ago.

  • Author
    Posts
  • #132135

    Participant
    Topics: 1
    Replies: 1
    Points: 14
    Rank: Member

    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!!

  • #132296

    Participant
    Topics: 2
    Replies: 25
    Points: 167
    Helping Hand
    Rank: Participant

    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/

  • #132299

    Participant
    Topics: 12
    Replies: 215
    Points: 380
    Helping Hand
    Rank: Contributor

    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.

  • #132354

    Participant
    Topics: 7
    Replies: 14
    Points: 25
    Rank: Member

    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

  • #133242

    Participant
    Topics: 1
    Replies: 1
    Points: 14
    Rank: Member

    @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!!

The topic ‘Exporting Multiline fields to CSV’ is closed to new replies.

denizli escort samsun escort muğla escort ataşehir escort kuşadası escort