profile for Gajendra D Ambi on Stack Exchange, a network of free, community-driven Q&A sites

Friday, July 31, 2015

Increment those IP addresses

Have you ever wanted to perform a certain kind of action on a range of ip addresses using powercli or powershell? Well, I have.
for ex:
foreach ($host in $hosts) {scriptblock}
and here I only have the 1st ip of the hosts and the rest should auto increment.
Well thanks to Lucd (If you have ever seen anyone answering all the powercli queries on vmware communities) we have it now.
#let's get the 1st ip address
$host1 = Read-Host "type the 1st ip address"

#Then let us have the number of ip addresses that you want
$max = Read-Host "Maximum nubmer of ip addresses?"

#now split that ip address and retain the 1st 3 octets which will be fixed
$fixed = $host1.Split('.')[0..2]

#now let us have the last octet of the host's ip address
$last = [int]($host1.Split('.')[3])

#let us subtract 1 from the maximum number of IPs that we want since it counts from 0
$max_hosts = $max - 1

#now let us increment the $last octet with a +1 till we get $max_hosts of ip addresses
$hosts =
$last..($last + $max_hosts) | %{
    [string]::Join('.',$fixed) + "." + $_

#now let us print the results to the screen

Get this IPincrementer on github.


  1. Thank you again for all the knowledge you distribute,Good post. I was very interested in the article, it's quite inspiring I should admit. I like visiting you site since I always come across interesting articles like this one.Great Job, I greatly appreciate that.Do Keep sharing! Regards,

  2. One important step that many people fail to make is changing the admin password. It is very easy to look up the default password for a certain brand of router.