Receive gateway from local network interface with PowerShell

25. November 2015

PowerShell Version: >1
Modules: none

I needed a way to log the first local interface’s gateway for debugging reasons, here is how I did it:

$netcards = Get-WmiObject Win32_NetworkAdapterConfiguration
# reset the array with every run of the script
$netcard_gateway_array = @()
    # ...we might have more than one network interface present
    foreach ($netcard in $netcards)
        # don't show interfaces without default gw
        if ($netcard.DefaultIPGateway -ne $null)
            # for more than one interface present we need an array to store the information
            $netcard_gateway_array += $netcard.DefaultIPGateway
# receive only the first interface's gateway

Leave a Reply

Your email address will not be published. Required fields are marked *