I want to get a sorted, formatted list of MAC Addresses from Hyper-V. So far, I've got:
get-vmnic | select Address | sort Address
which produces something like this:
Address-------00155D649A0100155D649A0200155D649A0300155D649A05
but what I'd like is this:
Address-------00-15-5D-64-9A-0100-15-5D-64-9A-0200-15-5D-64-9A-0300-15-5D-64-9A-05
How can I do this in the above one-liner?
You'd do this with select-object.
Get-VMNic | Select-Object -Property @{name='Address';expression={xxx}} | Sort-Object -Property Address
Whatever goes into 'xxx' would use '$_.Address' to access the original address property. Unfortunately, you're down to basic string manipulation, and PowerShell doesn't have a ton of great string manipulation capabilities. Just not its strong suit. So we'll probably have to use a method of the .NET System.String class. Assuming the existing address is a string...
$_.Address.Insert(2,'-').Insert(5,'-').Insert(8,'-').Insert(11,'-').Insert(14,'-')
Would go in place of 'xxx.' You may have to tweak the numeric values to get the insertion points right. See http://msdn.microsoft.com/en-us/library/system.string.insert.aspx for the Insert() method docs.
Here's another option using the -split operator:
PS> ('00155D649A01' -split '(..)' | Where-Object {$_}) -join '-'
00-15-5D-64-9A-01
And for all addresses:
Get-VMNic | Foreach-Object { ($_.Address -split '(..)' | Where-Object {$_}) -join '-' }