Sorting IPAddresses the PowerShell Way

A question came up on the PowerShell newsgroup about how to sort IP addresses in PowerShell.  The poster had down this in Linux like so:

#> cat ips | awk -F. ‘{printf("%03d.%03d.%03d.%03d\n", $1,$2,$3,$4)};’ |
sort -n -t "." | awk -F. ‘{printf("%d.%d.%d.%d\n", $1,$2,$3,$4)};’

Where the file "ips" contains the following IP addresses:

You could do the equivalent lexicographical sort in PowerShell like this:  Update: Jacques Barathon and MoW have shortened this approach quite a bit by doing:

> gc ips | sort {"{0:d3}.{1:d3}.{2:d3}.{3:d3}" -f @([int[]]$_.split(‘.’))

Which produces the correct output.  This solution does a string sort after converting each quad so that there are leading zeros which enables string sorting to give the correct answer e.g.:

And after the sort it round trips the string quads to ints and then reformats them as text without the leading zeros.  However this solution doesn’t take advantage of PowerShell’s unique ability to manipulate domain objects like say an IPAddress object which just happens to exists in the .NET Framework.  So my first take at solving this was to use the System.Net.IPAddress type:

> gc ips | %{[Net.IPAddress]$_} | sort Address | ft IPAddressToString


Note: It is not necessary to use the ‘System." prefix since PowerShell will do that for you if it can’t find the type.  Now the command above would have been a nice solution that takes advantage of PowerShell’s ability to manipulate objects – if it only worked.  Doh!  It turns out that the Address field value is in little endian byte order which won’t work for sorting.  We need the bytes to be interpreted in big endian order.  We can do this but it admittedly is a bit uglier:

> gc ips | %{[Net.IPAddress]::Parse($_)} | 
  sort {$b=$_.GetAddressBytes();[array]::Reverse($b);[BitConverter]::ToUInt32($b,0)} |
  ft IPAddressToString


What we really need is a BigEndianAddress property on the System.Net.IPAddress object.  Good luck trying to get the BCL team at Microsoft to add that anytime in the next several years.  🙂  Fortunately we don’t have to wait for them to do this.  PowerShell provides us with an extensible type system where we can add our own convenience properties to a pre-existing .NET object.  It is actually pretty easy.  First you need to construct an XML that tells PowerShell how to modify a particular .NET type:

<?xml version="1.0" encoding="utf-8" ?>

Save this to a file called IPAddress.ps1xml.  Now all you have to do is tell PowerShell to update its type data with the information in this file like so:

> Update-TypeData IPAddress.ps1xml

You might want to put this command in your profile so that the System.Net.IPAddress.BigEndianAddress is always available whenever you start up a new PowerShell session.  Now let’s use our new property to sort IP addresses:

> gc ips | %{[Net.IPAddress]$_} | sort BigEndianAddress | ft IPAddressToString


That is how to do sorting "the PowerShell way"!  Just remember – use the force, err I mean objects!

This entry was posted in PowerShell. Bookmark the permalink.

2 Responses to Sorting IPAddresses the PowerShell Way

  1. Bruce says:

    One other PowerShell feature that can help here is "covariant casts" where a collection of one type (string) can be cast into collection of a different type (IPAddress)  as long as each element in the collection can be converted. So if your file is:
    PS (1) > cat ips.txt124.
    Then you can sort by simply doing:
    PS (2) > [net.ipaddress[]] (cat ips.txt) | sort address | ft address, ipaddresstostring -auto
       Address IPAddressToString   ——- —————–  84215041 117768938 134612348
    You can use the same approach for other data types like dates:
    PS (3) > cat dates.txt07/04/177610/10/200701/01/1984PS (4) > [datetime[]] (cat dates.txt) | sort
    Thursday, July 04, 1776 12:00:00 AMSunday, January 01, 1984 12:00:00 AMWednesday, October 10, 2007 12:00:00 AM
    Bruce Payette [MSFT]

  2. Keith says:

    Ooh, covariant casts.  That\’s a nice tool to add to the toolbox. Thanks! 

Leave a Reply

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

You are commenting using your 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 )

Google+ photo

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

Connecting to %s