Skip to content

Instantly share code, notes, and snippets.

@realslacker
Last active June 22, 2018 15:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save realslacker/2d15d0aefc845bc70e1c63bdbd775012 to your computer and use it in GitHub Desktop.
Save realslacker/2d15d0aefc845bc70e1c63bdbd775012 to your computer and use it in GitHub Desktop.
<#
.SYNOPSIS
Reformats a MAC Address depending on the use case.
.DESCRIPTION
Takes a string or strings that should be reformated as a MAC address.
.PARAMETER Address
The MAC Address to reformat as a string
.PARAMETER Separator
What separator should be inserted, defaults to ':'
.EXAMPLE
Format-MACAddress '01-23-45-67-89-AF
.EXAMPLE
Add-CredentialToCache -Username user@domain.local
#>
[CmdletBinding(DefaultParameterSetName='Uppercase')]
param(
[Parameter(Position=0, Mandatory, ValueFromPipeline)]
[string[]]
$Address,
[Parameter(Position=1)]
[string]
$Separator = ':',
[Parameter(ParameterSetName='Uppercase')]
[switch]
$Uppercase,
[Parameter(ParameterSetName='Lowercase')]
[switch]
$Lowercase
)
process {
$Address | ForEach-Object {
$mac = $_ -replace '[^a-f0-9]', ''
if ( ( $_ -match '[^a-f0-9\s:-]' ) -or ( ( $mac.Length -ne 12 ) -and ( $mac.Length -ne 28 ) ) ) {
Write-Warning "May be an invalid MAC Address: $_"
}
$mac = $mac -replace '[^a-f0-9]', ''
$mac = $mac -replace '(..(?!$))',"`$1$Separator"
if ( $psCmdlet.ParameterSetName -eq 'Uppercase' ) { $mac.ToUpper() }
else { $mac.ToLower() }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment