PowerShell for Windows Admins

Oct 14 2015   5:07AM GMT

Modifying MAC address

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

Tags:
Powershell

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 for each 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

 Comment on this Post

 
There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when other members comment.

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to:

Share this item with your network: