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.
In part 1 we solved the puzzle by searching for and using commands that would directly deliver the data.
But what if you have no such command?
On Windows, the next best thing is researching and googling WMI (Windows Management Instrumentation). That is a service that can answer almost any question about logical and physical computer hard- and software.
It may take you a bit at google to find the WMI class that represents the information you are after, but once you found the class, WMI returns structured data and works locally and remotely:
PS C:\> Get-CimInstance -ClassName Win32_NetworkAdapter | Select-Object -Property Name, NetConnectionStatus, NetConnectionID, MACAddress Name NetConnectionStatus NetConnectionID MACAddress ---- ------------------- --------------- ---------- Microsoft Kernel Debug Network Adapter Killer(R) Wi-Fi 6 AX1650s 160MHz Wireless Network Adapter (201D2W) 7 WLAN 24:EE:9A:54:1B:E5 Microsoft Wi-Fi Direct Virtual Adapter 24:EE:9A:54:1B:E6 Hyper-V Virtual Switch Extension Adapter Bluetooth Device (Personal Area Network) 7 Bluetooth Network Connection 24:EE:9A:54:1B:E9 WAN Miniport (SSTP) WAN Miniport (IKEv2) WAN Miniport (L2TP) WAN Miniport (PPTP) WAN Miniport (PPPOE) WAN Miniport (IP) E6:A5:20:52:41:53 WAN Miniport (IPv6) 0A:9D:20:52:41:53 WAN Miniport (Network Monitor) 36:90:20:52:41:53 Microsoft Wi-Fi Direct Virtual Adapter #2 26:EE:9A:54:1B:E5 USB Ethernet 2 Ethernet 2 80:3F:5D:05:58:91 Hyper-V Virtual Ethernet Adapter 2 vEthernet (Default Switch) 00:15:5D:58:46:A8 Realtek USB GbE Family Controller PS C:\> Get-CimInstance -ClassName Win32_NetworkAdapter | Where-Object NetConnectionStatus -eq 2 | Select-Object Name, MACAddress Name MACAddress ---- ---------- USB Ethernet 80:3F:5D:05:58:91 Hyper-V Virtual Ethernet Adapter 00:15:5D:58:46:A8
The only thing that is changing for WMI is the WMI class name (Win32_NetworkAdapter in the previous case). Once you find your way into WMI, it is really simple. Get-CimInstance can answer your questions about MacAddresses, BIOS versions or even the free space on your drives:
PS C:\> Get-CimInstance -ClassName Win32_BIOS SMBIOSBIOSVersion : 1.9.1 Manufacturer : Dell Inc. Name : 1.9.1 SerialNumber : 4ZKM0Z2 Version : DELL - 20170001 PS C:\> Get-CimInstance -ClassName Win32_LogicalDisk DeviceID DriveType ProviderName VolumeName Size FreeSpace -------- --------- ------------ ---------- ---- --------- C: 3 OS 1007210721280 92713074688 Z: 4 \\192.168.2.106\scan scan 34546137268224 16054324969472
The takeaway for today :
- If you can’t find a good PowerShell cmdlet to provide you with the information you need, and if the information is related to computer management, then WMI is your next best choice.
- WMI delivers the same structured data that you get from PowerShell cmdlets
- WMI however is strictly Windows and not available cross-platform