Wait! There are JOINS in PowerShell???

And no I’m not talking about -join

Words: 278

Time to read: ~1.5 minute

Update: 2018-03-22 – Had the wrong link to Warren’s post on Join-Object; Added more examples on request.

Update: 2018-03-23 – Code and screenshot for LEFT JOIN was actually for FULL OUTER JOIN. Updated

This is probably going to be the shortest blog post that I write, mainly because it’s a link to another persons blog, but I found this so interesting that I felt I had to share it.

Now we all know that PowerShell has joins but, in this context, I’d call them simple joins


'This','is','how','we','join','stuff' -join '-'

view raw

SimpleJoin.ps1

hosted with ❤ by GitHub

Which gives us the lovely result of…

SimpleJoin
Simples, no?

That’s not what I’m talking about though!

What I’m talking about has even been talked about by the Microsoft PowerShell Team and what I’m talking about is Join-Object

Now I’m going to stop right here and just direct you to the blog post by Warren Frame ( blog | twitter ) where he talks all about it, why he wrote it, and how to use it.

I’ll leave you…

…with a quick example

LEFT JOIN


. .\Desktop\In_Progress\PowerShellScripts\Join-Object.ps1
$ThingsILike = 'Coffee','Whiskey','Food','ToMoveItMoveIt' | ForEach-Object -Process {
[PSCustomObject]@{
Name = $_
}
}
$ThingsICanDrink = 'Coffee;$true','Whiskey;$true','Food;$false' | ForEach-Object -Process {
$Name,$CanDrink = $_ -split ';'
[PSCustomObject]@{
Name = $Name
CanDrink = $CanDrink
}
}
$JoinParams = @{
Left = $ThingsILike
Right = $ThingsICanDrink
LeftJoinProperty = 'Name'
RightJoinProperty = 'Name'
Type = 'AllInLeft'
Prefix = 'InRight_'
}
Join-Object @JoinParams

LeftJoinExample

the other option was Sir Mix-a-Lot

INNER JOIN


$ThingsILike = 'Coffee', 'Whiskey', 'Food', 'ToMoveItMoveIt' | ForEach-Object -Process {
[PSCustomObject]@{
Name = $_
}
}
$ThingsICanDrink = 'Coffee;$true', 'Whiskey;$true', 'Food;$false' | ForEach-Object -Process {
$Name, $CanDrink = $_ -split ';'
[PSCustomObject]@{
Name = $Name
CanDrink = $CanDrink
}
}
$JoinParams = @{
Left = $ThingsILike
Right = $ThingsICanDrink
LeftJoinProperty = 'Name'
RightJoinProperty = 'Name'
Type = 'OnlyIfInBoth'
Prefix = 'InRight_'
}
Join-Object @JoinParams

InnerJoinExample
No moving allowed

FULL OUTER JOIN


$ThingsILike = 'Coffee', 'Whiskey', 'Food', 'ToMoveItMoveIt' | ForEach-Object -Process {
[PSCustomObject]@{
Name = $_
}
}
$ThingsICanDrink = 'Coffee;$true', 'Whiskey;$true', 'Food;$false', 'Ice;"…um…both?"' | ForEach-Object -Process {
$Name, $CanDrink = $_ -split ';'
[PSCustomObject]@{
Name = $Name
CanDrink = $CanDrink
}
}
$JoinParams = @{
Left = $ThingsILike
Right = $ThingsICanDrink
LeftJoinProperty = 'Name'
RightJoinProperty = 'Name'
Type = 'AllInBoth'
Prefix = 'InRight_'
}
Join-Object @JoinParams

FullOuterJoinExample
Ice, Ice, Baby!

Now don’t get me wrong…

…if there is enough data to import this into the database & use T-SQL then you can bet that’s what I’m going to do! It’s what it was designed for, I’d find it easier, and it’s probably going to be faster after you hit a certain threshold.

However, if it’s small sets and the effort of importing the data is going to slow you down and break your flow…

Well, that doesn’t have to be the case anymore.

Amazing what you can do with PowerShell 🙂

Author: Shane O'Neill

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

5 thoughts on “Wait! There are JOINS in PowerShell???”

Leave a Reply

Discover more from No Column Name

Subscribe now to keep reading and get access to the full archive.

Continue reading