PowerShell Genie

Export IPv4 configuration

Script 2 min read Windows PowerShell 5.1 and PowerShell 7

Documentation: Microsoft Learn

Get-NetIPAddress limited to -AddressFamily IPv4 returns every address bound to every interface.

The script

1PS> Get-NetIPAddress -AddressFamily IPv4 | Select-Object IPAddress,PrefixLength,InterfaceAlias,AddressState

Script reader

Inspect before you run

Inspection only

Inspect the script below. It does not run in this browser.

Inspection only — no command has run.

    Show reference output
    1 IPAddress PrefixLength InterfaceAlias AddressState
    2 --------- ------------ -------------- ------------
    3 10.20.30.11 24 Ethernet Preferred
    Inspect this example’s structure

    Read the command and its supplied arguments. This example requires real PowerShell or its module; this diagram does not execute it.

    Get-NetIPAddress -AddressFamily IPv4 | Select-Object IPAddress,PrefixLength,InterfaceAlias,AddressState
    1. Stage 1Get-NetIPAddress-AddressFamily
    2. Stage 2Select-ObjectPositional values or no named parameters

      How it works

      Get-NetIPAddress limited to -AddressFamily IPv4 returns every address bound to every interface. Select-Object reports the address with its PrefixLength, the InterfaceAlias it belongs to, and AddressState, which tells you whether the stack currently considers the address usable.

      Adapting it

      Add PrefixOrigin and SuffixOrigin to the property list to separate a statically assigned address from one handed out by DHCP, since the address value alone will not tell you which it is.

      If the result is unexpected

      The list includes an address in the 169.254 range, or an address that should be live reports an AddressState other than Preferred.

      An APIPA address means the interface requested a DHCP lease and did not receive one, so the stack self-assigned. AddressState reports the outcome of duplicate address detection, and Tentative or Duplicate means the address is configured but not usable for traffic. Both appear as ordinary rows, so a report reading only IPAddress misses the problem completely.

      1. Check AddressState on every row and treat anything other than Preferred as a finding.
      2. For an APIPA address, look at the DHCP client service and test reachability to the DHCP server or relay.
      3. Run Get-NetIPConfiguration for a combined view carrying the gateway and DNS servers alongside the address.
      4. Read PrefixOrigin to establish whether the address came from DHCP, a manual entry, or the well-known fallback.
      5. Test for a duplicate by pinging the address from another host while this interface is disabled.

      Validate the result

      Every interface meant to be in service reports a Preferred AddressState with a PrefixOrigin matching how it was configured.

      Keep the check reliable

      Include AddressState and PrefixOrigin in any address inventory, since the address on its own does not tell you whether it works.

      Primary documentation

      PowerShell objects and pipelines documentation

      Continue
      Genie guideExport IPv4 configuration

      Get-NetIPAddress limited to -AddressFamily IPv4 returns every address bound to every interface.