Creating Random MAC Addresses

If you just need a bunch of randomly generated MAC addresses, and you don’t care much about whether these addresses are actually valid, then here is a one liner:

 
PS> (0..5 | ForEach-Object { '{0:x}{1:x}' -f (Get-Random -Minimum 0 -Maximum 15),(Get-Random -Minimum 0 -Maximum 15)})  -join ':'

a5:66:07:6d:d9:18

PS> (0..5 | ForEach-Object { '{0:x}{1:x}' -f (Get-Random -Minimum 0 -Maximum 15),(Get-Random -Minimum 0 -Maximum 15)})  -join ':'

3c:c8:4e:e3:75:6c
 

Add it to a loop, and you get as many MAC addresses as you need:

 
PS> 0..100 | ForEach-Object { (0..5 | Foreach-Object { '{0:x}{1:x}' -f (Get-Random -Minimum 0 -Maximum 15),(Get-Random -Minimum 0 -Maximum 15)})  -join ':' }

bc:38:3a:91:a9:79
36:55:3a:a0:3d:c4
6d:2c:91:ae:01:35
ec:01:11:42:a7:09
e7:0b:24:d3:14:1d
(...)
 

Twitter This Tip! ReTweet this Tip!

  • Cool!

    This also works and runs over 10 times faster:

    [BitConverter]::ToString([BitConverter]::GetBytes((Get-Random -Maximum 0xFFFFFFFFFFFF)), 0, 6).Replace('-', ':')

    Or if you prefer lower case:

    [BitConverter]::ToString([BitConverter]::GetBytes((Get-Random -Maximum 0xFFFFFFFFFFFF)), 0, 6).Replace('-', ':').ToLower()

    Note that this will only work in a 64-bit PowerShell instance. In a 32-bit instance , the largest value that can be passed to Get-Random's Maximum parameter is [Int32]::MaxValue.

    Rick