Fixing reverse DNS

Hi, I’m trying to fix our missing DNS Reverse lookups with Powershell.

I have a CSV file with the headers ‘Name’ and last two octets of an ‘IP’ address that looks like below:


My goal is to reverse the IP value so it displays as 120.169

Then create a new object with the Name and reversed IP.


Then import the Name and reversed IP using the command below
The problem I am having is creating the new object with Name and reversed IP so I can capture that in a variable.

I figured out how to reverse the IP:
How can i create a new object with Name and IPrev that looks like below?


I would like to capture that to a variable $servers1 and | to Add-DnsServerResourceRecordPtr

****the commands below can add, get, and remove a single PTR record from DNS

#Creating Test Environment
$yourcsv = New-TemporaryFile
'@ |
Set-Content $yourcsv.FullName

# Create $servers objects with 3rd and 4th octet reversed.
$servers = Import-Csv $yourcsv |
            Select-Object -Property name, @{name="ip";e={($_.ip -split '\.')[1] + "." + ($_.ip -split '\.')[0]}}

In this example I used the -split operator to split the octets into an array then reversed the last two. The backslash is required because we want the literal . and it is a regular expression (. is any character).

Here’s another option using regular expression

$csv | ForEach-Object {
        Name  = $
        Iprev = $_.iprev -replace '(\d{3}).(\d{3})','$2.$1'

Note the period matches any character. We could’ve escaped it to be a literal period as well

$csv | ForEach-Object {
        Name  = $
        Iprev = $_.iprev -replace '(\d{3})\.(\d{3})','$2.$1'

Thanks, both of these examples worked for me.