# Combining Empty Arrays

**URL:** https://forums.powershell.org/t/combining-empty-arrays/9754
**Category:** PowerShell Help
**Created:** [November 22, 2017, 10:26am UTC](https://forums.powershell.org/t/combining-empty-arrays/9754 "2017-11-22T10:26:30Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![stackexchangeuser4](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/stackexchangeuser4/32/306_2.png) [@stackexchangeuser4](https://forums.powershell.org/u/stackexchangeuser4)
#### Post date: [November 22, 2017, 10:26am UTC](https://forums.powershell.org/t/combining-empty-arrays/9754/1 "2017-11-22T10:26:30Z")

</div>

I’ve been pulling my hair out at this one for the last few hours, seems to be down to an obscure quirk.

(EDITED)

Import this function:

```
Function Test-Filter
{
    [cmdletbinding()]
    Param
    (
        [Parameter(Mandatory = $true,
            ValueFromPipeline,
            Position = 0)]
        $Items
    )
    Process
    {
        $Items | Foreach-Object {
            if ($_ -eq "A")
            {
                $output += $_
            }
        }
    }
    End
    {
        if ($output.Count -gt 0)
        { Write-Output (,$output) }
    }
}
```

Now run this:

```
[Array]$arr1 = "A", "B", "C", "D" | Test-Filter | Sort
[Array]$arr2 = "E", "F", "G", "H" | Test-Filter | Sort | Select -First 1
[Array]$arr3 = 1, 2 | Test-Filter
[Array]$arr4 = "Jolly Cooperation" | Test-Filter

[Array]$combined = $arr1 + $arr2 + $arr3 + $arr4

$combined.count
```

Count is 1, as expected, $arr1 contains “A” and the rest are empty. Now run this:

```
Invoke-Command {
    [Array]$arr1 = "A", "B", "C", "D" | Test-Filter | Sort
    [Array]$arr2 = "E", "F", "G", "H" | Test-Filter | Sort | Select -First 1
    [Array]$arr3 = 1, 2 | Test-Filter
    [Array]$arr4 = "Jolly Cooperation" | Test-Filter

    [Array]$combined = $arr1 + $arr2 + $arr3 + $arr4

    $combined.count
}
```

Count is now 4.

This is part of a process to list patch files awaiting installation, there are four types of files but not all may be present in each patch type. Invoke-Command is required to format and log the output of the (lengthy) script but it seems to be messing with arrays somehow?

---

<div class="post-metadata">

### Author: ![sam-boutros](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/sam-boutros/32/5_2.png) [@sam-boutros](https://forums.powershell.org/u/sam-boutros)
#### Post date: [November 22, 2017, 10:32am UTC](https://forums.powershell.org/t/combining-empty-arrays/9754/2 "2017-11-22T10:32:13Z")

</div>

Answer is 4 not 1 in PS 5:

```
PS D:\Sandbox> [Array]$arr1 = "A"
[Array]$arr2 = $null
[Array]$arr3 = $null
[Array]$arr4 = $null

[Array]$combined = $arr1 + $arr2 + $arr3 + $arr4
$combined.Count
4

PS D:\Sandbox> $PSVersionTable

Name Value                                                                                                  
---- -----                                                                                                  
PSVersion 5.1.15063.726                                                                                          
PSEdition Desktop                                                                                                
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0...}                                                                                
BuildVersion 10.0.15063.726                                                                                         
CLRVersion 4.0.30319.42000                                                                                        
WSManStackVersion 3.0                                                                                                    
PSRemotingProtocolVersion 2.3                                                                                                    
SerializationVersion 1.1.0.1
```

To lose the null arrays replace the line

```
[Array]$combined = $arr1 + $arr2 + $arr3 + $arr4
```

with something like

```
[Array]$combined = @($arr1,$arr2,$arr3,$arr4) | % { if ($_) {$_} }
```

---

<div class="post-metadata">

### Author: ![stackexchangeuser4](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/stackexchangeuser4/32/306_2.png) [@stackexchangeuser4](https://forums.powershell.org/u/stackexchangeuser4)
#### Post date: [November 22, 2017, 10:37am UTC](https://forums.powershell.org/t/combining-empty-arrays/9754/3 "2017-11-22T10:37:47Z")

</div>

Edited OP with working example, I thought that one did work but as something (I think my AV) tried to shut my computer down without warning I can’t be sure.

---

<div class="post-metadata">

### Author: ![stackexchangeuser4](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/stackexchangeuser4/32/306_2.png) [@stackexchangeuser4](https://forums.powershell.org/u/stackexchangeuser4)
#### Post date: [November 22, 2017, 10:47am UTC](https://forums.powershell.org/t/combining-empty-arrays/9754/4 "2017-11-22T10:47:08Z")

</div>

Oh, and just to further confuse things, with the above Test-Filter function:

```
Invoke-Command {
    [Array]$arr1 = "A", "B", "C", "D" | Test-Filter | Sort
    [Array]$arr2 = "E", "F", "G", "H" | Test-Filter | Sort | Select -First 1
    [Array]$arr3 = 1, 2 | Test-Filter
    [Array]$arr4 = "Jolly Cooperation" | Test-Filter

    [Array]$combined = $arr1 + $arr2 + $arr3 + $arr4

    Set-Variable -Name "DebugTest" -Value $combined -Scope Global

    $combined.Count
}

1
```

That one caused me some grief trying to debug it =/

I’ve used a Where { $\_ } which seems a bit tidier but still really weird.

---

<div class="post-metadata">

### Author: ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)
#### Post date: [May 16, 2024, 8:37pm UTC](https://forums.powershell.org/t/combining-empty-arrays/9754/5 "2024-05-16T20:37:10Z")

</div>


