Functions/Data/Format-MAC.ps1
<#
.Synopsis Processes an Input MAC Address in any standard notation and outputs into selected notation. .DESCRIPTION This script takes in a MAC Address, strips out all the normal delimiters, tests whether or not the inpt is a standard 6 byte string and then outputs the results into the selected notation style. .EXAMPLE Format-MAC -MAC "E4:43:4B:18:3D:A8" .EXAMPLE Format-MAC -MAC "E4:43:4B:18:3D:A8" -Notation "Colon6" #> function Format-MAC { [CmdletBinding()] Param ( # MAC Address to Reformat [Parameter(Mandatory=$true,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true)] [string] $MAC, # Notation to use in reformatting. All three standard notations are present as well as Raw undelimited output. [Parameter(Mandatory=$false)] [ValidateSet("Colon6","Hyphen6","Period3","Raw")] [string] $Notation = "Colon6" ) Process { $RawMAC = $MAC -replace ':','' -replace '\.','' -replace '-','' if ($RawMAC.Length -eq 12) { $FormMac = switch ($Notation) { 'Colon6' {"{0}:{1}:{2}:{3}:{4}:{5}" -f ((0..5) | foreach-object {$RawMAC.Substring(($_*2),2)})} 'Hyphen6' {"{0}-{1}-{2}-{3}-{4}-{5}" -f ((0..5) | foreach-object {$RawMAC.Substring(($_*2),2)})} 'Period3' {"{0}.{1}.{2}" -f ((0..2) | foreach-object {$RawMAC.Substring(($_*4),4)})} 'Raw' {$RawMAC} } $FormMac } else {Write-Error "The input MAC Address ($MAC) is not a complete regular 6 Byte Address" -ErrorAction Continue} } } |