PowerShell offers you a plentitude of approaches to solve a task. They always boil down to the same strategies. In this mini-series, we will illustrate them all one by one.
Here’s the problem to solve: get the MAC address of your computer.
The easiest way to tackle the problem is to find a command that can do this for you. Researching commands via Get-Command is the first step:
PS C:\> Get-Command -Name *get*mac* CommandType Name Version Source ----------- ---- ------- ------ Function Get-DbaSchemaChangeHistory 1.1.143 dbatools Function Get-NetworkControllerMacPool 1.0.0.0 NetworkController Function Get-VIMachineCertificate 12.4.1.... VMware.PowerCLI.VCenter Function Invoke-GetVmClassNamespaceManagementVirtualMach... 1.0.104... VMware.Sdk.vSphere.vCenter.NamespaceManagement Cmdlet Get-AlarmAction 12.4.0.... VMware.VimAutomation.Core Cmdlet Get-AlarmActionTrigger 12.4.0.... VMware.VimAutomation.Core Cmdlet Get-PnPCustomAction 1.12.27 PnP.PowerShell Cmdlet Get-PnPCustomAction 3.29.21... SharePointPnPPowerShellOnline Application getmac.exe 10.0.19... C:\WINDOWS\system32\getmac.exe
Getmac.exe sounds promising, and it can easily solve the puzzle:
PS C:\> getmac Physical Address Transport Name =================== ========================================================== 24-EE-9A-54-1B-E5 Media disconnected 24-EE-9A-54-1B-E9 Media disconnected 80-3F-5D-05-58-91 \Device\Tcpip_{44FE83FC-7565-4B18-AAC8-AB3EC075E822} 00-15-5D-58-46-A8 \Device\Tcpip_{DC7C0CE2-B070-4070-B5CB-1C400DA69AF8}
Unfortunately, getmac is of command type “Application” so it is an executable. This means to you:
- It may not be available on another platform
- Results are plain text and not structured
If *reading* the MAC address off screen is sufficient and if you deal with Windows only, you may be happy. Else, the next step would be searching for a better command by extending your search criteria and/or googling.
Soon it will turn out there’s also a PowerShell cmdlet called Get-NetAdapter that does the trick:
PS C:\> Get-NetAdapter Name InterfaceDescription ifIndex Status MacAddress LinkSpeed ---- -------------------- ------- ------ ---------- --------- vEthernet (Default Swi... Hyper-V Virtual Ethernet Adapter 28 Up 00-15-5D-58-46-A8 10 Gbps WLAN Killer(R) Wi-Fi 6 AX1650s 160MHz Wir... 15 Disconnected 24-EE-9A-54-1B-E5 780 Mbps Ethernet 2 USB Ethernet 11 Up 80-3F-5D-05-58-91 1 Gbps Bluetooth-Netzwerkverb... Bluetooth Device (Personal Area Netw... 9 Disconnected 24-EE-9A-54-1B-E9 3 Mbps PS C:\> Get-Command -Name Get-NetAdapter | ft -AutoSize CommandType Name Version Source ----------- ---- ------- ------ Function Get-NetAdapter 2.0.0.0 NetAdapter
Get-Command reveals: it is a “function”. Both “function” and “cmdlet” resemble PowerShell commands (commonly simplified as cmdlets). That’s good because it means to you:
- The command most likely works cross-platform
- The results are object-oriented (structured)
Since cmdlets return structured data, it’s easy for you to access particular data like the MacAddress by specifying its “column” (or property):
PS C:\> (Get-NetAdapter).MacAddress 00-15-5D-58-46-A8 24-EE-9A-54-1B-E5 80-3F-5D-05-58-91 24-EE-9A-54-1B-E9
All PowerShell cmdlets ship in modules, so if you found a useful cmdlet, make sure you also take a look at the related cmdlets in the same module:
PS C:\> Get-Command -Module (Get-Command -Name Get-NetAdapter).Source CommandType Name Version Source ----------- ---- ------- ------ Function Disable-NetAdapter 2.0.0.0 NetAdapter Function Disable-NetAdapterBinding 2.0.0.0 NetAdapter Function Disable-NetAdapterChecksumOffload 2.0.0.0 NetAdapter Function Disable-NetAdapterEncapsulatedPacketTaskOffload 2.0.0.0 NetAdapter Function Disable-NetAdapterIPsecOffload 2.0.0.0 NetAdapter ... }
The takeaway for today:
- Take your time to search for commands. Finding a command close to what you want is so much easier than using generic commands and reinvent the wheel
- Try to use PowerShell cmdlets. Applications work, too, but only as a last resort