concept range operator in category powershell

appears as: rge operator
Windows PowerShell in Action, Third Edition

This is an excerpt from Manning's book Windows PowerShell in Action, Third Edition.

The foreach statement iterates over a collection of objects, and the range operator is a way to generate a sequence of numbers. The two combine to make looping over a sequence of numbers a very clean operation.

Because the range operator generates a sequence of numbers, and numbers are objects like everything else in PowerShell, you can implement this using pipelines and the ForEach-Object (alias foreach) cmdlet:

This is because the range operator has higher precedence than the addition operator.

4.4.4. Using the range operator with arrays

There’s one other tool in the indexing toolkit: the range operator discussed in the previous section. This operator is a convenient way to get slices of arrays. Say you have an array of ten elements, with values 0–9. To get the first four elements of an array, you can use the range operator as follows:

sitemap

Unable to load book!

The book could not be loaded.

(try again in a couple of minutes)

manning.com homepage
test yourself with a liveTest