title: Increment counter inside Foreach-Object loop
body: <p>I want to output each line in a text document into its own document where the output documents are named 1.txt, 2.txt, etc.
  This is my initial code that reads the text documents and outputs the lines one by one.</p>
  
  <pre><code>get-content "artists.txt" | Foreach-Object {$_ | set-content "artists\$counter.txt"}
  </code></pre>
  
  <p>But as you can imagine, this outputs always to the same document.</p>
  
  <p>I have tried </p>
  
  <pre><code>get-content "artists.txt" | 
    Foreach-Object {$_ | set-content "artists\$counter.txt" } {$counter++}
  </code></pre>
  
  <p>as I have seen suggested online and while this increases the counter, it doesn't do what I want (since it still outputs to the same file). </p>
  
  <p>Not really sure where to insert the counter++ here as inside the Foreach-Object loop itself gives me an error (unless I'm inserting it wrong).</p>
  
  <p>Thanks.</p>