Monthly Archive

Categories

Modifying MAC address

 

Another question on the forum brought up an interesting point. User want adapter name and mac address

PS> Get-NetAdapter | select Name, MacAddress

Name                         MacAddress
----                         ----------
WiFi                         28-18-78-D0-05-11
Bluetooth Network Connection 28-18-78-D0-05-12

 

but wanted to remove the hyphens in the mac address so tried

PS> Get-NetAdapter | select Name, MacAddress | foreach {$_.MacAddress -replace '-',''}
281878D00511
281878D00512

 

This doesn’t work because the foreach cmldet only knows to put put the new string when the hyphens have been replaced in the mac address. The name is effectively filtered out.

 

The answer is to use a calculated field in select-object like this

PS> Get-NetAdapter | select Name, @{N='MacAddress'; E={$_.MacAddress -replace '-',''}}

Name                         MacAddress
----                         ----------
WiFi                         281878D00511
Bluetooth Network Connection 281878D00512

 

N = Name and E= Expression though they are usually shorten for brevity as shown

Comments are closed.