0

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.

Jeroen
  • 60,696
  • 40
  • 206
  • 339
Bryce Conley
  • 21
  • 1
  • 1
  • 2

4 Answers4

9

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.

Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
2

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".

Aacini
  • 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
2

Another option for you.

for($i=10; $i -le 1000; $i=$i+5){$i | Out-File -Append sequence.txt}
Squashman
  • 13,649
  • 5
  • 27
  • 36
  • How is this executed in the command-line? `powershell` followed by what? – Aacini Dec 01 '15 at 20:04
  • @Aacini, are you asking how to execute it from cmd.exe? – Squashman Dec 01 '15 at 20:07
  • 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
1

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
}
Chris N
  • 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