0

I have around 20 arrays which contain over 100 values each.

I want to output these to a csv file with column headings.

If I type any of these arrays in a powershell command prompt they display on multiple lines and I can select different items from the array using $arrayname{14] for example, so I think they are being stored correctly.

If I use the following line in my script:

"" | select-object @{Name="Column1"; Expression={"$Array1"}},@{Name="Column2"; Expression={"$Array2"}},@{Name="Column3"; Expression={"$Array3"}} | export-csv $exportLocation -notypeinformation

Then it creates the columns with the heading but each array variable is displayed on one line.

How can I get the output to display the arrays in the respective columns on a line of their own?

1 Answer 1

2

You need to convert your 4 arrays into an array of objects with 4 properties. Try this:

$Array1 = @(...)
$Array2 = @(...)
$Array3 = @(...)
$Array4 = @(...)

$len1 = [Math]::Max($Array1.Length, $Array2.Length)
$len2 = [Math]::Max($Array3.Length, $Array4.Length)
$maxlen = [Math]::Max($len1, $len2)

$csv = for ($i=0; $i -lt $maxlen; $i++) {
  New-Object -Type PSCustomObject -Property @{
    'Column1' = $Array1[$i];
    'Column2' = $Array2[$i];
    'Column3' = $Array3[$i];
    'Column4' = $Array4[$i];
  }
}

$csv | Export-Csv 'C:\path\to\output.csv'
Sign up to request clarification or add additional context in comments.

2 Comments

Hi, thanks for helping. Unfortunately its not outputting anything. The arrays hold the following commands: $Array1 = @(dsget group "CN=OU1,OU=Departments,OU=Groups,DC=contoso,DC=com" | dsquery user -limit 1000) $Array2 = @(dsget group "CN=OU2,OU=Departments,OU=Groups,DC=contoso,DC=com" | dsquery user -limit 1000) $Array3 = @(dsget group "CN=OU3,OU=Departments,OU=Groups,DC=contoso,DC=com" | dsquery user -limit 1000) $Array4 = @(dsget group "CN=OU4,OU=Departments,OU=Groups,DC=contoso,DC=com" | dsquery user -limit 1000)
@user2615378 My bad. I didn't fix all variable names after I copy/pasted my code. Please try again.

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.