Union and Intersection in PowerShell?
Assuming $arr
is the array, you can do like this:
$computers = $arr | select -expand computer -unique$arr | group uid | ?{$_.count -eq $computers.count} | select name
In general, I would approach union and intersection in Powershell like this:
$a = (1,2,3,4)$b = (1,3,4,5)$a + $b | select -uniq #union$a | ?{$b -contains $_} #intersection
But for what you are asking, the above solution works well and not really about union and intersection in the standard definition of the terms.
Update:
I have written pslinq which provides Union-List
and Intersect-List
that help to achieve set union and intersection with Powershell.
You can also do
$a = (1,2,3,4)$b = (1,3,4,5)Compare-Object $a $b -PassThru -IncludeEqual # unionCompare-Object $a $b -PassThru -IncludeEqual -ExcludeDifferent # intersection
Doesn't work if $a
is null though.