In Powershell, what's the best way to join two tables into one?

Michael Steele picture Michael Steele · Dec 4, 2009 · Viewed 29.8k times · Source

I'm fairly new to Powershell, and am wondering if someone knows of any better way to accomplish the following example problem.

I have an array of mappings from IP address to host-name. This represents a list of active DHCP leases:

PS H:\> $leases

IP                    Name
--                    ----
192.168.1.1           Apple
192.168.1.2           Pear
192.168.1.3           Banana
192.168.1.99          FishyPC

I have another array of mappings from MAC address to IP address. This represents a list of IP reservations:

PS H:\> $reservations

IP                    MAC
--                    ---
192.168.1.1           001D606839C2
192.168.1.2           00E018782BE1
192.168.1.3           0022192AF09C
192.168.1.4           0013D4352A0D

For convenience, I was able to produce a third array of mappings from MAC address to IP address and host name using the following code. The idea is that $reservations should get a third field, "Name", which is populated whenever there's a matching "IP" field:

$reservations = $reservations | foreach {
    $res = $_
    $match = $leases | where {$_.IP -eq $res.IP} | select -unique
    if ($match -ne $NULL) {
        "" | select @{n="IP";e={$res.IP}}, @{n="MAC";e={$res.MAC}}, @{n="Name";e={$match.Name}}
    }
}

The desired output is something like this:

PS H:\> $ideal

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

Is there any better way of doing this?

Answer

iRon picture iRon · Aug 3, 2017

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

Main features:

  • Intuitive (SQL like) syntax
  • 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 40% faster than 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 can be download from PowerShell Gallery using the command:

Install-Script -Name Join

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

After downloading (Install-Script -Name Join), the script can simply be invoked by dot sourcing:

. .\Join.ps1

You might also consider to convert the script to a PowerShell module by renaming it to a PowerShell module (.psm1) file and moving it to a one of the module folders defined in $env:PSModulePath. For more details see: How to Write a PowerShell Script Module.
Note: the Import-Module command is required to load the proxy commands.

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

Examples

More examples can be found in the related Stackoverflow questions at:

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)