Using powershell, I'm trying to append a sequence of numbers starting at 10, and incrementing by 5 up to 1000. The appending has to go to a text tile, say sequence.txt
.

- 60,696
- 40
- 206
- 339

- 21
- 1
- 1
- 2
4 Answers
Another variant:
10..1000 | Where-Object { $_ % 5 -eq 0 } | Set-Content 'sequence.txt'
10..1000
creates a sequence of integers from 10 to 1000. The Where-Object
then removes all numbers that aren't divisible by 5 from that list. Set-Content
writes the result to a file.

- 193,178
- 25
- 254
- 328
-
I think I like this solution the most.. no for loops or anything.. everyone likes the PowerShell "one liner" – Chris N Dec 01 '15 at 20:38
-
2My thinking was `2..200 | % { $_ * 5 }`, but this is about the same. – Bacon Bits Dec 01 '15 at 20:52
This is a Batch file solution; copy it to a file with .bat extension:
@echo off
(for /L %%i in (10,5,1000) do echo %%i) > sequence.txt
This Batch file contains practically the exact same parts of your description: "a sequence of numbers starting at 10, and incrementing by 5 up to 1000 --> append the sequence to sequence.txt".

- 65,180
- 12
- 72
- 108
-
This answer included [for completeness](http://stackoverflow.com/questions/32417191/batch-file-to-delete-half-the-lines-of-a-text-file-at-random/32432287#comment52948712_32432287), I assume. – Ryan Bemrose Dec 01 '15 at 20:30
-
1@RyanBemrose: No. The original question had the `batch-file` tag (that was the reason because I see it). Jeroen edit just removed it... – Aacini Dec 01 '15 at 20:35
Another option for you.
for($i=10; $i -le 1000; $i=$i+5){$i | Out-File -Append sequence.txt}

- 13,649
- 5
- 27
- 36
-
How is this executed in the command-line? `powershell` followed by what? – Aacini Dec 01 '15 at 20:04
-
-
Yes! I always had problems understanding the multiple options that PS have in this case, and I think the OP have the same problems... – Aacini Dec 01 '15 at 20:09
-
@Aacini, like this: `C:\>powershell -c "for($i=10; $i -le 1000; $i=$i+5){$i | Out-File -Append sequence.txt}"` – Squashman Dec 01 '15 at 20:13
-
Wow, it works! This is the first PS example I can run in a simple way! I have seen many examples with "executionpolices" and several other complicated options... – Aacini Dec 01 '15 at 20:17
-
@Aacini, or this: `C:\>powershell -c "for($i=10; $i -le 1000; $i=$i+5){$i}" >Sequence.txt` – Squashman Dec 01 '15 at 20:18
-
Wow, works tremendously - and even tried it using CMD and works flawlessly. I appreciate all the help, and wish I had a basic course before the one im surrently in, so I atleast wasnt thrown in blind. – Bryce Conley Dec 01 '15 at 20:31
You start a PowerShell script by using the ISE or a prompt.. look on the Internet for examples. Since you're having issues with the very basics, like setting a variable (that in this case has a starting value of 10) and then "looping" "until" it hits another value, your issues aren't PowerShell... they're basic programming. Using google, you can figure out how to set a variable, how to make a while loop, etc.. in any language, not just PowerShell.
Anyway, this should get you started, once you find the PowerShell shell or ISE...
$i = 10
while ($i -le 1000){
$i | Out-File -Append test.txt
$i += 5
}

- 7,239
- 1
- 24
- 27
-
Thanks, for my degree - this is my first programming class of any sort. (Networking) the only other code ive seen thus far is SQL, its been a difficult road for me in this class thus far, as everything seems so foreign to me. I appreciate the help tremendously on getting started. Thank you. – Bryce Conley Dec 01 '15 at 19:45