0

I have an array and i want to export it to CSV and I use below code.

[string[]] $Params = "Foo", "Bar"
$Params | out-file "D:\Output.csv" -Encoding ascii -Force -Append

#Output
Foo
Bar
Foo1
Bar1
Foo2
Bar2

It appends the CSV in row wise and i want it to in column wise like below,

Foo, Bar
Foo1, Bar1
Foo2, Bar2

Somebody please help to get a solution...

2
  • Hi, see my answer. If that's not what you want, please provide more context so we can try to help you (such as maybe a bigger array sample / additional details) . Commented Feb 7, 2020 at 12:04
  • Thanks Sage.. Am a newbie, For me its little difficult to understand Powershell objects. i have tried your code to convert to csv, but it returns length of the strings. Commented Feb 7, 2020 at 13:29

5 Answers 5

1

If all you want in the result is to combine the 1st value in the array with the 2nd and so on, a simple for loop would do:

[string[]] $Params = "Foo", "Bar","Foo1", "Bar1","Foo2", "Bar2"
$result = for ($i = 0; $i -lt $Params.Count -1; $i+=2 ) {
    # if any of the strings in the $Params array contains spaces 
    # or comma's I would strongly suggest quoting the output.
    '"{0}","{1}"' -f $Params[$i], $Params[$i+1]
}

# output on screen
$result

# output to CSV (no headers)
$result | Set-Content -Path 'D:\output.csv'

Output of the above:

"Foo","Bar"
"Foo1","Bar1"
"Foo2","Bar2"
Sign up to request clarification or add additional context in comments.

Comments

0

You need to use ConvertTo-CSV then output to file or ExportTo-Csv.

For that to work, you will use it on a collection of object.

Here's a randomly generated array of PSObject that contains a Foo and Bar property. Once I have the object (you would use yours instead), you just have to do like I did in the last line.

Example

$Array = for ($i = 0; $i -lt 10; $i++) {
    [PSCustomObject]@{
        Foo = "Meh-$(Get-Random -Minimum 0 -Maximum 20)"
        Bar = get-random
    }    
}

# The actual thing that matter
$Array | ConvertTo-Csv -NoTypeInformation | out-file "D:\Output.csv" -Encoding ascii -Force 

Random CSV output result

RandomCSVOutputResult

Comments

0

Take this:

[string[]] $params = "Foo", "Bar"

$params -join ', ' | out-file "D:\Output.csv" -Encoding ascii -Force -Append

1 Comment

Its working fine, the only drawback is its writing everything in a cell. We are going to use this CSV file as a data seeding in Performance testing. So let me check whether its working or not.. Thank you for your efforts..
0
$params = "Foo", "Bar"
$params -join ', ' | Out-File C:\temp\test.csv

And then use Text to columns filtering on the , in Excel.

Comments

0

You could write the csv file manually yourself(I find this gives you more control in getting the output you want):

[string[]] $Params = "Foo", "Bar"
$fileStream = [System.IO.StreamWriter]::new("D:\Output.csv",1)
foreach($p in $Params){
    $fileStream.Write($p+",")
}
$fileStream.Write("`n")
$fileStream.Close()

4 Comments

Thank you... Its overwriting the exist values, i want to append.
@Enock, I edited my answer that it should now append to the file. Here is a link to documentation for the .net StreamWriter class. learn.microsoft.com/en-us/dotnet/api/…
Thank you for your efforts.. but its writing all the values by column wise. I want to write first two elements column wise, for next iteration it should move to next row and write the values column wise..
@Enock, so you just have to write a new line character where you need it. I edited my code above as an example.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.