Really simple Group & Sort question

I’ve been learning PowerShell from the jumpstart course on MVA - and I am loving it! Snover said it best when he said it makes doing admin work fun again. I’m trying to completely rely on the tool (both to help me learn it better, and because it’s a lot better than any other tool I’m using right now).

My question is a simple one, not obviously solved by the get-help files. I hope this is the appropriate place to post it.

Using Get-NetIPAddress and trying to both sort the results and group them. What am I doing wrong?

This works great:

Get-NetIPAddress |sort InterfaceIndex |ft InterfaceIndex, InterfaceAlias, AddressFamily, IPAddress -AutoSize

However, I really want to group IPv4 and IPv6 to make the list easier to read (and to get more practice with group and sort). When I add Group, I get a table with the headers and no content (which is weird).

Get-NetIPAddress |Group-Object -Property AddressFamily |sort InterfaceIndex |ft InterfaceIndex, InterfaceAlias, Add
ressFamily, IPAddress -AutoSize

The results aren’t what I expect at all:

InterfaceIndex InterfaceAlias AddressFamily IPAddress
-------------- -------------- ------------- ---------

Can you offer any help/advice - even just a help file or example to seek. I’m trying to do some much more complicated things with group and sort on our network - but if I don’t want to move on to more complicated things until I understand what’s happening here.

Thanks in advance!

‘Group’ is not meant to sort a list of records in an array, that’s what ‘sort’ does.

What you’re trying to do is to sort by AddressFamily, then sort by InterfaceIndex. This can be done by:

Get-NetIPAddress |sort AddressFamily,InterfaceIndex | ft InterfaceIndex, InterfaceAlias, AddressFamily, IPAddress -AutoSize

Grouping returns a record for every addressfamily (2 records in this case, 1 for IPv4 and 1 for IPv6) as in:

Get-NetIPAddress | group AddressFamily

The properties of the ‘group’ output are Count, Name, and Group. This explains why if you pipe that to ‘sort InterfaceIndex’, it will not sort since you’re asking to sort a collection based on a non-existing property. Similarly, ‘Format-Table InterfaceIndex, InterfaceAlias, AddressFamily, IPAddress’ will return nothing since the object piped from the ‘group’ command does not have any of these properties…

Ahh! Makes perfect sense. Thanks so much for the quick reply.

Not sure how I missed it, but didn’t realize you could use multiple properties for sort.

Thanks again.

Also want to add that if you are working with a fairly large data set you can limit it by further piping to | select -first/last 1. This will give you the headers and the data set. For the example above it is unlikely it will be a ton of data. But lets say you were doing something similar against a 50MB csv.

So:

Get-NetIPAddress |sort AddressFamily,InterfaceIndex | select -first 1 | ft InterfaceIndex, InterfaceAlias, AddressFamily, IPAddress -AutoSize