Tuesday, April 14, 2015

Generate the list of IP-Addresses using PowerShell Range operator.

 

main

PowerShell Range Operator.

I was busy in writing my book on "Automating Microsoft Azure with PowerShell" that's why was not able to write blogs in previous months. I am hoping that you guys missed me ;o) . In-case if you didn't missed me, that's fine too..

Yesterday our own  "A! Murky Ana" call me late night ( she called me after a long time, and not sure where she was been, anyway...) and asked me that if it s feasible to generate  a list of address using PowerShell.

I said , YES, it is feasible, it is just a single line of code to achieve that task. You need to use the "PowerShell Range Operator". PowerShell range operator is mentioned by double dots (..)

The syntax is simple, write the range in which you want to start, this should be integer and then type two dots and the end of the range integer.

For example, if you want to create a range of numbers from 1 to 10, then you just need to type 1..10 and it will generate the list of numbers started from 1 till 10.

1

Now, let's talk about generating IP-Addresses using PowerShell. To generate a range of PowerShell IP-Addresses, we need to use two commands, one the is the range operator, and second is the Foreach-Object , and off course the pipe too, to join both commands.

1..250 | ForEach-Object {"192.168.1.$_"}

 

RangeOperator

 

In above command, we are first generating a list of range of numbers from 1-to-250 , and the we are piping the output to the Foreach-Object cmdlet, and in for each script block we are looping through every number, and then Concatenation to 192.168.1.$_ , the $_ variable is replacing by the value of the integer.

 

Simple... ;o) . No? by using a  single line of code, we have generated a list of Ip-Addresses.

 

 

Check the video of PowerShell Range Operator

 

 

tumblr_njiml4gOlY1rku136o1_500

 

* All characters appearing in this work are fictitious Any resemblance to real persons, living or dead, is purely coincidental.

 

Regards.

Aman Dhally
If you like, you can follow me on Twitter and Facebook. You can also check my “You Tube channel for PowerShell video tutorials. You can download all of my scripts from “Microsoft TechNet Gallery”.

1 comment:

  1. Thank you so much!! This takes care of many, many hours of hand jamming crunch numbers. Awesome!!!

    ReplyDelete