digitalbunny
digitalbunny

Reputation: 33

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

Upvotes: 3

Views: 3203

Answers (3)

Philippe
Philippe

Reputation: 1

this post is a quite old, but I'm facing the same need, so I found a way by using a Class:

    Class MyRow
{
  [string] $col1
  [string] $col2
  [string] $col3
}
MyList = new-object System.collections.arraylist


$Currentrow = new-object MyRow
$currentrow.Col1 ="Col1"
$currentrow.Col2 ="Col2"
$currentrow.Col3 ="Col3"
Mylist.add($currentrow)


Mylist | out-gridview

Upvotes: 0

Santiago Squarzon
Santiago Squarzon

Reputation: 60883

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 objects from a embebed CSV structured string instead of += is to use Here-Strings:

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

Upvotes: 4

skeetastax
skeetastax

Reputation: 1718

Use this:

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

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

Et viola!

Upvotes: 0

Related Questions