Extract IP Addresses from report using the 2nd Octet

Hello all,

This is my first forum post. I’m extremely new to scripting in general as well as powershell. I apologize ahead of time if I don’t have the correct terminology or comprehension level needed for this.

Ultimate Goal:
We have multiple buildings where the naming convention for PCs is different per building. We perform a lot of inter-building moves and I would like to clean up computer names that have moved from one building to another.

What I’ve done so far:
I’ve ran this cmdlet in Powershell;
Get-ADComputer -Filter ‘Name -like “BTP”’ -Properties IPv4Address | FT Name,DNSHostName,IPv4Address -A | Out-File C:\New.Txt

So this generates a list of Computer Names, their DNS Host names (which I could technically omit) and their IP addresses;
BLBTP81 BLBTP81.domain.local
BLBTP80 BLBTP80.domain.local

I’ve also used the Export-CSV cmd, however I do not know how to separate the results with a comma so it puts all the data into a single column.

What I need help with:
I’ll take whatever suggestions anyone would like to give. What I’ve been trying to do is parse the text file that I’ve created with the cmdlet above, so that it filters out ONLY those computer names and IP addresses in which the 2nd octet is 27. So I could either remove all results that start with 10.28 or just parse out to a new text file those that start with 10.27

Again, I was going to just try to use the CSV file and sort/filter out what I needed, but then I’m not really using powershell to it’s full extent, nor do I know how to separate the results with a comma.

Thank you for your time!

You’ll definitely want to use CSV files rather than text files with table-formatted text. Parsing the latter can be a pain. Try this:

Get-ADComputer -Filter 'Name -like "*BTP*"' -Properties IPv4Address |
Select-Object -Property Name,DNSHostName,IPv4Address |
Export-Csv -NoTypeInformation -Path C:\New.csv

Once that’s done, you can re-import the CSV file anytime you like to fiddle with the data further. For example:

Import-Csv -Path C:\New.csv |
Where-Object { $_.IPv4Address -like '*.27.*.*' }

So, your first problem is Format-Table. Once you’ve formatted something, you’re done. See, “Big Book of PowerShell Gotchas” on the Resources>Ebooks tab. Formatting is for when you want to look at the data on-screen or put it into a non-delimited, hard-to-parse, human-readable text file. If that’s not what you want, you don’t format.

What you probably want to start with is Select-Object. You specify your list of properties (Name, DNSHostName, IPv4Address), and then you can pipe to Export-CSV and get everything nicely separated into columns.


Get-ADComputer -Filter 'Name -like "*BTP*"' -Properties IPv4Address |
Select-Object -Prop Name,DNSHostName,IPv4Address

What you want to do now, you said, is filter on the second IP address octet. No problem - PowerShell does filtering. But if you’re sticking data in a text file and then parsing that, you’re doing it wrong. There’s probably a LOT of ways to do what you’re after, but I’ll pick the one I like.

Get-ADComputer -Filter 'Name -like "*BTP*"' -Properties IPv4Address |
Select-Object -Prop Name,DNSHostName,IPv4Address |
ForEach-Object {
  $parts = $_.IPv4Address -split "."
  if ($parts[1] -eq '27') { $_ }

I’m splitting the IP address into an array, using the periods as delimiters. So, each octet ends up in its own array element. Those start at number 0, so number 1 is the second octet. If that’s 27, I output the original object (which has Name, DNSHostName, and IPv4Address properties).

If you knew the first octet was always 10, you could be simpler.

Get-ADComputer -Filter 'Name -like "*BTP*"' -Properties IPv4Address |
Select-Object -Prop Name,DNSHostName,IPv4Address |
Where-Object { $_.IPv4Address -like '10.27.*' }

Simple string match. This assumes the IPv4Address property is a string. I think it is. I’m not positive.

As for Export-CSV, if you’re doing it right, you don’t need to separate the results with a comma. That’s what the command does. If it isn’t doing it, then you’re not feeding it what it needs - often, because the results were formatted first.

I’ll take a moment to selfishly recommend Learn Windows PowerShell in a Month of Lunches as a good starting point for figuring this stuff out.

WOW! Mr. Wyatt and Mr. Jones… I had no idea I was reaching out to some real Powershell hard hitters! Thank you so much for your assistance. With your help I’ve accomplished exactly what I needed.

On a side note, I already have your book Learn Windows PowerShell in a Month of Lunches, now I just need to make the time to go through it… and even harder than that, I need to have an environment where my account permissions are not so limited, but you can’t help with that!

I have also heard good things about Big Book of PowerShell Gotchas, I’ll look into obtaining that as well.


So, that last book is one of our freebies. You can get it right now - at the top of the site, select Resources on the menu and pick ebooks.