title: Split string with PowerShell and do something with each token
body: <p>I want to split each line of a pipe on spaces, and then print each token on its own line.</p>
  
  <p>I realise that I can get this result using:</p>
  
  <pre><code>(cat someFileInsteadOfAPipe).split(" ")
  </code></pre>
  
  <p>But I want more flexibility. I want to be able to do just about anything with each token. (I used to use <a href="http://en.wikipedia.org/wiki/AWK">AWK</a> on Unix, and I'm trying to get the same functionality.)</p>
  
  <p>I currently have:</p>
  
  <pre><code>echo "Once upon a time there were three little pigs" | %{$data = $_.split(" "); Write-Output "$($data[0]) and whatever I want to output with it"}
  </code></pre>
  
  <p>Which, obviously, only prints the first token. Is there a way for me to for-each over the tokens, printing each in turn?</p>
  
  <p>Also, the <code>%{$data = $_.split(" "); Write-Output "$($data[0])"}</code> part I got from a blog, and I really don't understand what I'm doing or how the syntax works.</p>
  
  <p>I want to google for it, but I don't know what to call it. Please help me out with a word or two to Google, or a link explaining to me what the <code>%</code> and all the <code>$</code> symbols do, as well as the significance of the opening and closing brackets.</p>
  
  <p>I realise I can't actually use <code>(cat someFileInsteadOfAPipe).split(" ")</code>, since the file (or preferable incoming pipe) contains more than one line.</p>
  
  <p>Regarding some of the answers:</p>
  
  <p>If you are using <a href="https://technet.microsoft.com/en-us/library/hh849903.aspx"><code>Select-String</code></a> to filter the output before tokenizing, you need to keep in mind that the output of the <code>Select-String</code> command is not a collection of strings, but a collection of <code>MatchInfo</code> objects. To get to the string you want to split, you need to access the <code>Line</code> property of the <code>MatchInfo</code> object, like so:</p>
  
  <pre><code>cat someFile | Select-String "keywordFoo" | %{$_.Line.Split(" ")}
  </code></pre>