PowerShell & Determining Correct IP Address based on Gateway

Hi Guys,

I’m working on a script which I’m trying to determine the IP Address based on matching the gateway IP. I want to gather info for an IP Scan I’m planning to use in my script but in cases which the computer has multiple IP addresses I need to capture the correct one. In most cases there is only one gateway so wanting to use this as my measure to get the correct IP.

Any suggestions of code?

Also a side note any suggestions on best way to obtain IP range of a subnet based on IP address using PowerShell?


You are working on … !? … so, what have you tried so far? Please show your code. We might be able to sort it out or to extend/improve the code to fit your requirements.

At the moment have no code…just looking around for examples at the moment. I’m using CIM Instance win32_networkAdapter or networkadapterconfiguration to pull information. Looking for something that is compatible for all versions of PS

Cool. So why don’t you come back when you have something to show? There are a lot of great sources for inspiration for Powershell code. For example the PowershellGallery oder StackOverflow. … and mostly we are way better with correcting or improving code than to write it from scratch for someone else. :wink:

Ah … and btw … I wouldn’t bother to write my code to be compatible with ALL version of Powershell. There are actually only two versions left - 5.1 and 7.x. :wink:

OK Will come back with some code soon…

I would get the CIM Instance that has the default gateway set and grab the IP address:

Get-CimInstance Win32_NetworkAdapterConfiguration | Where-Object {$_.DefaultIPGateway -ne $null} | Select-Object -ExpandProperty IPAddress

Might need a tweak if IPv6 is enabled as you’ll get that address too.

I’m not aware of any easy way to calculate the range in PowerShell but the list of subnet masks is finite so I guess you could just create a list, do a look up and work out your start and end address.

Have a look at this site:




[quote quote=287056]I would get the CIM Instance that has the default gateway set and grab the IP address:

<textarea class="urvanov-syntax-highlighter-plain print-no" style="tab-size: 4; font-size: 14px !important; line-height: 18px !important; z-index: 0; opacity: 0;" readonly="readonly" data-settings="dblclick">Get-CimInstance Win32_NetworkAdapterConfiguration | Where-Object {$_.DefaultIPGateway -ne $null} | Select-Object -ExpandProperty IPAddress</textarea>
Get-CimInstance Win32_NetworkAdapterConfiguration | Where-Object {$_.DefaultIPGateway -ne $null} | Select-Object -ExpandProperty IPAddress

This command doesn’t help when network card has two IP addresses assigned. I’m trying to find a way when multiple IPs are assigned that get the same IP that matches the gateway so we know its the primary IP/network.

This is how I’m thinking

  • Get Default Gateway into a variable or Split octets' from Gateway
  • Find best command to get IP Address that matches first two octets' of gateway. Third octect can be different depending on subnet so left that out.
  • Once have correct IP/Subnet that is based on the gateway I can then work out on IPScanner script
  • Otherwise is there a way to define which is the "primary" ip address in powershell. Or strip the ipconfig /all information and use the first IP address which is normally the one needed.

Yeah, good point. I was working on the assumption you had multiple adapters with their own IP.

You could get all the IPs then filter based on a wildcard (replace the last octet or last two octets with ‘*’).

$ips = '','',''
$gateway = ''
$ips | Where-Object {$_ -like $($gateway -replace '\d$','*')}


Let me add one more point.

You cannot only depend on the gateway as a reference. take this example

Your script will fail if you have a classless network such as or nothing similar cause the gateway will have a different range.

before writing your script and reaching a dead-end, are you are that you are only using a Classful (A-B-C) Subnet mask

Thanks for the suggestions…It’s really hard to determine what will be the primary IP address so I might make my script prompt the user to enter or select which Network ID based on IP Addresses found.

I’ll convert IPs to Network IDs and then use a choice menu for selection. This then can be used to work out IP range to scan (the main reason for my script)


I might be able to determine primary IP address when there is two or more IP Address on an adapter.

Using Registry which lists the primary IP always first under HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\Interfaces

as well was using Get-CIMInstance -Class Win32_NetworkAdapterConfiguration -Filter IPEnabled=TRUE | Where-Object {$.DefaultIPGateway -ne $null} | Select-Object -ExpandProperty IPAddress | where {$ -notmatch ‘:’}