Is there a UNION in PowerShell

Words: 462 487

Time to read: ~ 3 minutes

TL;DR: [System.Collections.Generic.HashSet<T>]

It has been a while ago since I’ve blogged so it seems fitting that this post will be about a question that was asked of me a while ago. The question was along the lines of “Can I join objects in PowerShell but remove duplicates?”.

So this is allowed:

1
2
3
4

But this isn’t allowed:

1
2
3
1

For me, it summed down to “Is there something like UNION or UNION ALL in PowerShell?

Luckily, this is something that I had asked before and been told the answer. So here I am, repeating the answer for you all since this is how I learn; repetition and practice.

The PowerShell type:

[System.Collections.Generic.HashSet<T>]
Don’t worry about the ‘<T>’. I didn’t figure it out either but I’ve been informed that it means a generic Type.

UPDATE: I have been reliably informed that this is a major understatement and I will update when I know more/learn more/am taught more about this.

https://docs.microsoft.com/en-us/dotnet/api/system.collections.generic.hashset-1?view=netframework-4.7.2

An Example, not a Speech.

Let’s say we have similar but different objects.

$Boom = 1
$Blast = 2
$And = 3
$Ruin = 4

Now the question that we are asked is, if there is anyway to do a UNION on these objects?

Absolutely, we’ll create a hashset object and start putting these objects into the HashSet. I know these objects are integers so I’m going to put [Int] in for the <T> type.

#Create the HashSet object.
$HashSet = [System.Collections.Generic.HashSet[Int]]::new()

#Add the objects.
foreach ($Item in $Boom,$Blast,$And,$Ruin) {
    $HashSet.Add($Item)
}
Verbose by default

Now when you add something to a HashSet using the .Add method it returns either a True or a False.

Returns
Boolean
true if the element is added to the HashSet<T> object; false if the element is already present.

https://docs.microsoft.com/en-us/dotnet/api/system.collections.generic.hashset-1.add?view=netframework-4.7.2#System_Collections_Generic_HashSet_1_Add__0_

If we then check the HashSet object, it returns both of our objects.

#Show us what you GOT!!!
$HashSet
and a 1 and a 2 and a…

If you don’t want to see the output of the .Add() method then you can push the output to one of the nulls e.g. $null = , [void] , > $null, | Out-Null, with the first two being placed at the start of the line and the last two at the end.

# Clear the HashSet.
$HashSet.Clear()

# Add items and hide output.
$null = foreach ($Item in $Boom,$Blast,$And,$Ruin) {
    $HashSet.Add($Item)
}

# See if it still worked.
$HashSet
Hide and seek…

Check for Duplicates.

Now let’s see what happens if we try to add a duplicate object to the HashSet.

#Clear the HashSet
$HashSet.Clear()

#Populate it again.
foreach ($Item in $Boom,$Blast,$And,$Boom){
    $HashSet.Add($Item)
}

#Check it again
$HashSet
No I don’t want no duplicates

HashSet sees the duplicate value, gracefully says no (False), and does not add it.

But wait, there’s more!

HashSet is something that I think DBAs will like as it is based on Sets. If you have the time, check out some of the other methods that it has.

#What else you got?
Get-Member -InputObject $HashSet
A set that contains everything except itself.

Take a look!

Author: Shane O'Neill

DBA, T-SQL and PowerShell admirer, Food, Coffee, Whiskey (not necessarily in that order)...

3 thoughts on “Is there a UNION in PowerShell”

    1. Yup, that seems to work! 👍 And being honest, if I was only joining two non-large arrays, then I’m 100% behind you and I’d use Select-Object and -Unique as well.
      There’s a couple of extra methods that HashSet has that I want to dive into more – plus the age old question on how to they perform against each over; Select -Unique vs. HashSet
      But yeah that method should work and it’s nice and clear! 🙂

      Like

What's your opinion?

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s