PowerShell_unix PowerShell is definitely gaining momentum in the windows scripting world but I still hear folks wanting to rely on Unix based tools to get their job done.  In this series of posts I’m going to look at converting some of the more popular Unix based tools to PowerShell.

seq

The Unix “seq” command prints a sequence of numbers from FIRST to LAST, in steps of INCREMENT.

The PowerShell “range” operator “..” will return an array of numbers but it does not give you the ability to modify the increment from 1 (or –1 if the LAST is smaller than FIRST).  I accomplished this with a simple for loop.  Since the comparison needed to be “less than or equal” for a increasing sequence and “greater than or equal” for a decreasing sequence, I executed the “for” loop with the Invoke-Expression command setting the comparison operator with a variable.  This avoided having to code two different for loops. 

Next, if the “equal width” argument is true, the max width is calculated from the FIRST and LAST numbers and the generated array is iterated through, modifying the entries if their widths were below the maximum.

Finally the array is either converted to a string with the supplied separator, or displayed to the console with entries separated by newlines.

The Unix parameters map to the following in my PowerShell script:

Unix PowerShell Description
-s -separator Use STRING to separate numbers (default crlf).
-w -equal_width equalize width by padding with leading zeros.
FIRST -first The start of the sequence.
INCREMENT -increment The steps between items.
LAST -last The end of the sequence.