One way to do it is with a for
loop.
$Array1 = 1, 2, 3
$Array2 = 'Joe Bloggs', 'John Doe', 'Jane Doe'
$Array3 = 'Yes', 'No'
$export = for($i = 0; $i -lt [Linq.Enumerable]::Max([int[]] ($Array1.Count, $Array2.Count, $Array3.Count)); $i++) {
[pscustomobject]@{
Number = $Array1[$i]
Name = $Array2[$i]
Valid = $Array3[$i]
}
}
$export | Export-Csv path\to\csv.csv -NoTypeInformation
Another example using a function, the logic is more or less the same except that there is more overhead involved, since this function can handle an indefinite amount of arrays coming from the pipeline.
function Join-Array {
[CmdletBinding()]
param(
[parameter(ValueFromPipeline, Mandatory)]
[object[]] $InputObject,
[parameter(Mandatory, Position = 0)]
[string[]] $Columns
)
begin {
$inputDict = [ordered]@{}
$index = 0
}
process {
try {
if ($MyInvocation.ExpectingInput) {
return $inputDict.Add($Columns[$index++], $InputObject)
}
foreach ($item in $InputObject) {
$inputDict.Add($Columns[$index++], $item)
}
}
catch {
if ($_.Exception.InnerException -is [ArgumentNullException]) {
$errorRecord = [Management.Automation.ErrorRecord]::new(
[Exception] 'Different count between input arrays and Columns.',
'InputArrayLengthMismatch',
[Management.Automation.ErrorCategory]::InvalidOperation,
$InputObject
)
$PSCmdlet.ThrowTerminatingError($errorRecord)
}
$PSCmdlet.ThrowTerminatingError($_)
}
}
end {
foreach ($pair in $inputDict.GetEnumerator()) {
$count = $pair.Value.Count
if ($count -gt $max) {
$max = $count
}
}
for ($i = 0; $i -lt $max; $i++) {
$out = [ordered]@{}
foreach ($column in $inputDict.PSBase.Keys) {
$out[$column] = $inputDict[$column][$i]
}
[pscustomobject] $out
}
}
}
The usage would be pretty easy, the arrays to join / zip can be passed through the pipeline and the desired column names defined positionally:
$Array1 = 1, 2, 3
$Array2 = 'Joe Bloggs', 'John Doe', 'Jane Doe'
$Array3 = 'Yes', 'No'
$Array4 = 'hello', 'world', 123, 456
$Array1, $Array2, $Array3, $Array4 | Join-Array Number, Name, Valid, Test |
Export-Csv path\to\csv.csv -NoTypeInformation
for
loop to a variable in that way, though in hindsight it makes some sense if each iteration just leaves a value on the pipeline. I'll definitely be using this technique more in the future. – Stilted