Batch file to compare ip address with registry key

Posted on 2014-08-08
Last Modified: 2014-08-08

i have software that puts ip address in registry key such as:


Key has ip address:

I want to compare the 'network' portion of the ip address in this key to a 'static network portion' such as 192.168.1  (/24) for this part i can set variable such as staticnetwork=192.168.1 and if registry contains this string as a positive match then i need to make an action such as install.exe

If the ip address in the key matches the correct network portion, then i need to perform action. and if not then i take separate action.

can you show me how this is accomplished??
Question by:zequestioner
    LVL 82

    Expert Comment

    Please refine your description of the registry part, because it's not quite clear currently.
    Do you have a key (a "folder" in regedit) as in "HKLM\Software\Acme\", with some values in there, or dou you have a key like "HKLM\Software\Acme" with a value "IPAddress" and the data ""?
    Could you post a regedit export or a screenshot (with sensitive information removed, of course)?
    LVL 14

    Expert Comment

    by:Brad Groux
    This should be possible with PowerShell and WMI (Get-WmiObject Win32_NetworkAdapterConfiguration).

    The Lonely Administrator has a great write up about how to get IP data using a function -

    You can then use PowerShell's conditional logic to perform the if/then tasks -

    Get-ItemProperty Cmdlet looks like to be the right track -

    To learn more about PowerShell check out Microsoft Virtual Academy's fantastic free courses -
    LVL 43

    Accepted Solution

    You can do something like this in batch.... change the key/value to the one you want:


    @echo off
    SET Key="HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\SystemInformation"
    Set Value="SystemProductName"
    for /f "tokens=3" %%a in ('reg query %Key% /v %Value%^| find %value%') do set IPAddress=%%a
    echo IP Address found is %ipaddress%
    if "%ipaddress:~0,10%"=="192.168.1." (
      ECHO On subnet 192.168.1
      ECHO do something here
      EXIT /B
    REM check any more subnets here etc.

    Open in new window

    LVL 1

    Author Comment

    I apologize for not being as specific, it is a registry key of type REG_SZ.

    Steve, when I put in the key path and set the value to the 'Value Name' of the key and the Value Data is the IP address, the ip does not match the subnet even though it does. I think i figure it out b/c your ip has 10 cahracters and mine has 12 so now i got it to work just fine. thank you for the help.

    Brad thank you for your help but steve solution is very easy and straightforward to figure out.

    Thank you steve this is exactly what i need and is perfect solution.
    LVL 43

    Expert Comment

    by:Steve Knight
    No problem, glad to help.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Enabling OSINT in Activity Based Intelligence

    Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

    The use of stolen credentials is a hot commodity this year allowing threat actors to move laterally within the network in order to avoid breach detection.
    If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
    In this video, we discuss why the need for additional vertical screen space has become more important in recent years, namely, due to the transition in the marketplace of 4x3 computer screens to 16x9 and 16x10 screens (so-called widescreen format). …
    Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    12 Experts available now in Live!

    Get 1:1 Help Now