Search code examples
powershellcsvout-gridview

Powershell CSV Variable to Out-GridView


This is driving me nuts. I want to save CSV text to a string variable - NOT a file! I want to then output that (CSV) text to the Out-GridView using the commas as column separators.

How do I do this? I have looked high and low, and I can find nothing - NOTHING - on how to do this without saving the text to CSV file first then importing it using Import-Csv. I don't want to save a file, I just want to display my CSV formatted multiline string object (with NewLine characters) in the Out-GridView.

Any clues?

Example:

$rows += "COL1,COL2,COL3`n"    # CSV header row AS TEXT
$rows += "val1,val2,val3`n"    # CSV data row AS TEXT

$rows | Out-GridView           # does not work! No columns :( - just a single column per row

Solution

  • Use this:

    $rows += "COL1,COL2,COL3`n"
    $rows += "val1,val2,val3`n"
    
    $csv = $rows | ConvertTo-Csv -Delimiter ","
    $csv | Out-GridView
    

    Et viola!