Parse IP Address to get the PTR address

Welcome Forums General PowerShell Q&A Parse IP Address to get the PTR address

This topic contains 4 replies, has 3 voices, and was last updated by

5 years, 4 months ago.

  • Author
  • #9326

    Points: 0
    Rank: Member

    In the perpetual pursuit of being the laziest admin on the planet I am creating a de-provisioning script for servers when we retire them. One of the things I am trying to automate is the cleanup of DNS of any statically assigned records (A, CNAME, MX, etc...)

    To get the info needed about a server name:

    dnscmd $DomainContoller /enumrecords $ServerName

    Now I need a way to read the ip of: and translate it to the zone of

    for an output like this:

    This is way over my feeble programming brain. any input will be met with the happy dance....


  • #9345

    Points: 1
    Rank: Member

    Something like this should do:

    PS C:\> $ipAddress = ''
    PS C:\> $ipAddress -replace '^(\d+)\.(\d+)\.\d+\.(\d+)$','$3.$2.$'

    Brief explanation:
    This uses a regular expression search and replace. It breaks the IP address into 4 parts (the 4 integer values, discovered by \d+, which means one or more digits, separated by \., which means '.' but needs to be escaped by backslash because '.' has special meaning in regular expressions), and captures the first, second and last parts that it discovers (capture is done by enclosure in round brackets). Once it has captured those values, it replaces the entire string with a new string, using the 3rd, 2nd, and 1st captured values separated by periods, followed by

  • #9348

    Points: 0
    Rank: Member

    Ouch. Now I gots dain brammage....

    It works very well. Now I am off to fish...

    Thanks for the solution AND the method.


  • #9358

    Points: 1
    Rank: Member


    The solution I provided is nice in terms of being short and to the point, but regular expressions are not intuitive and come with a steep learning curve.

    Here's another (regular expression-free) solution that will likely help you solve problems like this a little more easily on your own as you do more with PowerShell:

    # Start with an IP address
    $ipAddress = ''
    # Now lets split the address into its parts by using the dot ('.')
    $ipAddressParts = $ipAddress.Split('.')
    # Now reverse the array in place
    # For the zone, we only care about the first 2 and last 1 part of the ip address, so lets get those
    $importantIpAddressParts = $ipAddressParts | Select-Object -First 2 -Last 1
    # Now that we have those parts, lets join them with '.' and add the zone suffix
    $zone = [string]::Join('.',$importantIpAddressParts) + ''
    # Now output the zone

    This is a good example that shows the power of regular expressions though, because what I do here in many lines of script I can accomplish in a single line with a regular expression like I did earlier. Granted, I could have made this approach shorter by combining some of the steps, but I wanted to flush it out so that you could see what is being done each step of the way.

  • #9455

    Points: 324
    Helping Hand
    Rank: Contributor

    Or further simplified, after splitting the array just use a string format or string concantenation...

    # Now lets split the address into its parts by using the dot (‘.’)
    $ipAddressParts = $ipAddress.Split('.')

    # Use String Format....
    "{0}.{1}.{2}" -f $ipAddressParts[3], $ipAddressParts[2], $ipAddressParts[0]
    # Or other string concantenation
    $ipAddressParts[3] + "." + $ipAddressParts[2] + "." + $ipAddressParts[0] + ""

The topic ‘Parse IP Address to get the PTR address’ is closed to new replies.