Skip to content

Instantly share code, notes, and snippets.

@bcdady
Created May 1, 2015 21:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bcdady/a03c0506c7e68710b8d4 to your computer and use it in GitHub Desktop.
Save bcdady/a03c0506c7e68710b8d4 to your computer and use it in GitHub Desktop.
PowerShell One-Liner: Enumerate all DHCP enabled Network Adapters via WMI / CIM, using an embedded CIM query as the -Filter
# Step 1, filter network adapters to those that are IP & DHCP enabled
# - this can only be done with the Win32_NetworkAdapterConfiguration class:
Get-CimInstance -ClassName Win32_NetworkAdapterConfiguration -Filter 'IpEnabled=True AND DhcpEnabled=True'
# To get a unique handle to any/all network adapters matching the filtered query, retrieve only their Index propery:
(Get-CimInstance -ClassName Win32_NetworkAdapterConfiguration -Filter 'IpEnabled=True AND DhcpEnabled=True').Index
# Using the Index property, enumerate objects in the Win32_NetworkAdapter class, with the same Index (aka DeviceID) property:
# - notice the prior Get-CimInstance query gets wrapped in another $(), to force returning only the resulting value into this Filter statement
# - I added | Format-List, because the default Table formatted results truncated the value of the .Name property
Get-CimInstance -ClassName Win32_NetworkAdapter -Filter "Index=$((Get-CimInstance -ClassName Win32_NetworkAdapterConfiguration -Filter 'IpEnabled=True AND DhcpEnabled=True').Index)" | Format-List
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment