The other day, I was creating a web site and I need to create a drop down list for the site with values of 1-150.
<option>1</option>
<option>2</option>
<option>3</option>
etc…
I was faced with 2 choices, do this manually 150 times or find a way to automate it. Thinking about this, I wrote the following one-liner and then copied and pasted the output to my HTML document. Voila! Issue solved in less than 2 minutes.
PS C:\Temp> 1..150 | ForEach-Object { " <option>$_</option>"} > htmloptions.txt
I am the furthest thing from a PowerShell expert, but I try to think of ways to automate functions that I have to do more than 1 time.
The 1..150 defines a numeric range where number 1-150 would have normally been displayed on the screen.
Since I followed this up with the pipe symbol (not the letter I or L) | the output goes to the next cmdlet. This cmdlet ForEach-Object takes an action on each one of these numbers. This action was to write the options for the dropdown list. the $_ took the number that was piped in and inserted it in that location.
If you found this useful, please let me know in the comments section.
-Allen