Exporting a Wide Column to CSV

Welcome Forums General PowerShell Q&A Exporting a Wide Column to CSV

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

1 year, 10 months ago.

  • Author
  • #67489

    Points: 0
    Rank: Member

    Hi fellow Powershellers,

    I am a newbee to PS. Reading "powershell in a month of lunches". I am trying to determine a way to export values with a wide column, to a csv, and not have PS truncate a wide value. I have read the help files and have tried numerous basic (non-vb script approach) solutions – none doing what I want.

    I want to be able to dump something and send it out to an csv – simple and I can do that but when one of my columns is quite large, it will truncate the output (....). Here is an example which is "similar" to one of the book labs.

    dir 'C:\Windows\*.exe' | Format-Table name, @{name='Size';expression={$_.length}}, versioninfo | out-file .\myfile2.csv

    Now of course the above is a table output and the versioninfo field is huge. It stays truncate when I send it out to CSV. -autosize and -wrap do not suffice.

    When I run below using a list. Yes, it works but I do not want a list format. I want a simple column format:
    dir 'C:\Windows\*.exe' | Format-List name, @{name='Size';expression={$_.length}}, versioninfo | Out-file .\myfile.csv

    I was hoping that a simple override would reformat the output and bypass the width limitations based on the screen/default formatting.

    Yes, have read the help files and examples for format-table, format-list, out-file, export-csv and do not see a solution for this.

    What am I missing?
    Thanks MG

  • #67492

    Points: 642
    Helping Hand
    Rank: Major Contributor

    1. You should use format cmdlets only in the console as the very last part of the pipeline.
    2. out-file does not create a csv file by default ... there is a special cmdlet for that
    3. you should make sure to export only single "properties" to csv "cells" not arrays of properties.

    Try this:

    Get-Item 'C:\Windows\*.exe' | Select-Object -Property name, length, @{name='Version';expression={$_.VersionInfo.FileVersion}}  -OutVariable Executables
    $Executables | Export-Csv -Path .\myfile2.csv -NoTypeInformation

    Edit: Instead of "FileVersion" you also could use "ProductVersion" like this:

    Get-Item 'C:\Windows\*.exe' | Select-Object -Property name, length, @{name='Version';expression={$_.VersionInfo.ProductVersion}}  -OutVariable Executables
    $Executables | Export-Csv -Path .\myfile2.csv -NoTypeInformation

    Edit2: The explanation why the versioninfo is truncated in your result file is: VersionInfo is not a single property it is an array of properties. You will see this when you pick a single exe or dll file and pipe it to a Format-List * ... like this:

    Get-Item -Path 'C:\Windows\system32\*.exe' | Select-Object -first 1  | Format-List *
  • #67540

    Points: 0
    Rank: Member

    HI Olaf
    Thanks for the guidance. Now I understand.
    got it working fine.

The topic ‘Exporting a Wide Column to CSV’ is closed to new replies.

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