Home > Back-end >  Powershell CSV Variable to Out-GridView
Powershell CSV Variable to Out-GridView

Time:12-02

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

CodePudding user response:

Pipe $rows to ConvertFrom-Csv:

$rows  = "COL1,COL2,COL3`n"
$rows  = "val1,val2,val3`n"
$rows | ConvertFrom-Csv | Out-GridView

What you would normally want to do when creating a CSV like string instead of = to a string variable is to use Here-Strings:

@'
COL1,COL2,COL3
val1,val2,val3
'@ | ConvertFrom-Csv | Out-GridView

CodePudding user response:

Use this:

$rows  = "COL1,COL2,COL3`n"
$rows  = "val1,val2,val3`n"

$csv = $rows | ConvertTo-Csv -Delimiter ","
$csv | Out-GridView

Et viola!

  • Related