In PowerShell, what’s the best way to join two tables into one?

After 1.5 years, the cmdlet I had pasted in the original answer has undergone so many updates that it has become completely outdated. Therefore I have replaced the code and the ReadMe with a link to the latest version.

Join-Object

Combines two object lists based on a related property between them.

Description
Combines properties from one or more objects. It creates a set that can be saved as a new object or used as it is. An object join is a means for combining properties from one (self-join) or more object lists by using values common to each.

Main features

  • An intuitive idiomatic PowerShell syntax
  • SQL like joining features
  • Smart property merging
  • Predefined join commands for updating, merging and specific join types
  • Well defined pipeline for the (left) input objects and output objects (preserves memory when correctly used)
  • Performs about twice as fast as Compare-Object on large object lists
  • Supports (custom) objects, data tables and dictionaries (e.g. hash tables) for input
  • Smart properties and calculated property expressions
  • Custom relation expressions
  • Easy installation (dot-sourcing)
  • Supports PowerShell for Windows (5.1) and PowerShell Core

The Join-Object cmdlet reveals the following proxy commands with their own (-JoinType and -Property) defaults:

  • InnerJoin-Object (Alias InnerJoin or Join), combines the related objects
  • LeftJoin-Object (Alias LeftJoin), combines the related objects and adds the rest of the left objects
  • RightJoin-Object (Alias RightJoin), combines the related objects and adds the rest of the right objects
  • FullJoin-Object (Alias FullJoin), combines the related objects and adds the rest of the left and right objects
  • CrossJoin-Object (Alias CrossJoin), combines each left object with each right object
  • Update-Object (Alias Update), updates the left object with the related right object
  • Merge-Object (Alias Merge), updates the left object with the related right object and adds the rest of the new (unrelated) right objects

ReadMe

The full ReadMe (and source code) is available from GitHub: https://github.com/iRon7/Join-Object

Installation

There are two versions of this Join-Object cmdlet (both versions supply the same functionality):

  • Join Module
Install-Module -Name JoinModule
  • Join Script
Install-Script -Name Join

(or rename the Join.psm1 module to a Join.ps1 script file)
and invoked the script by dot sourcing:

. .\Join.ps1

Answer

To answer the actual example in the question:

$reservations |LeftJoin $leases -On IP

IP          MAC          Name
--          ---          ----
192.168.1.1 001D606839C2 Apple
192.168.1.2 00E018782BE1 Pear
192.168.1.3 0022192AF09C Banana
192.168.1.4 0013D4352A0D

Performance

A little word on performance measuring:
The PowerShell pipeline is designed to stream objects (which saves memory), meaning that both¹ lists of input objects usually aren’t (shouldn’t be) resident in memory. Normally they are retrieved from somewhere else (i.e. a remote server or a disk). Also, the output usually matters where linq solutions are fast but might easily put you on the wrong foot in drawing conclusions because linq literally defers the execution (lazy evaluation), see also: fastest way to get a uniquely index item from the property of an array.
In other words, if it comes to (measuring) performance in PowerShell, it is important to look to the complete end-to-end solution, which is more likely to look something like:

 import-csv .\reservations.csv |LeftJoin (import-csv .\leases.csv) -On IP |Export-Csv .\results.csv

(1) Note: unfortunately, there is no easy way to build two parallel input streams (see: #15206 Deferred input pipelines)

(more) Examples

More examples can be found in the related Stack Overflow questions at:

  • Combining Multiple CSV Files
  • CMD or Powershell command to combine (merge) corresponding lines from two files
  • Can I use SQL commands (such as join) on objects in powershell, without any SQL server/database involved?
  • Powershell match properties and then selectively combine objects to create a third
  • Compare Two CSVs, match the columns on 2 or more Columns, export specific columns from both csvs with powershell
  • Merge two CSV files while adding new and overwriting existing entries
  • Merging two CSVs and then re-ordering columns on output
  • Efficiently merge large object datasets having multiple matching keys
  • How to compare two CSV files and output the rows that are in either of the file but not in both
  • How to join two CSV files in Powershell with SQL LIKE syntax

side-by-side join examples

  • Combine two CSVs – Add CSV as another Column
  • Is there a PowerShell equivalent of paste (i.e., horizontal file concatenation)?
  • How can merge 3 cycle and export in one table
  • Merging two arrays object into one array object in powershell
  • s there a way to transform horizontally formatted CSV output to vertical when converting XML to CSV in PowerShell?
  • How to import this Json data as a flat object rather than a nested object

And in the Join-Object test script.

Please give a 👍 if you support the proposal to Add a Join-Object cmdlet to the standard PowerShell equipment (#14994)

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)