2

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

3 Answers3

3

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
Santiago Squarzon
  • 41,465
  • 5
  • 14
  • 37
0

Use this:

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

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

Et viola!

skeetastax
  • 1,016
  • 8
  • 18
0

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
Philippe
  • 1
  • 1
  • Thank you for contributing to the Stack Overflow community. This may be a correct answer, but it’d be really useful to provide additional explanation of your code so developers can understand your reasoning. This is especially useful for new developers who aren’t as familiar with the syntax or struggling to understand the concepts. **Would you kindly [edit] your answer to include additional details for the benefit of the community?** – Jeremy Caney Jul 27 '23 at 00:25