title: Why will Powershell write to the console, but not to a file?
body: <p>I have a PowerShell script that sets flags based on various conditions of the file. I'll abbreviate for brevity.   </p>
  
  <pre><code>$path = "c:\path"
  $srcfiles = Get-ChildItem $path -filter *.htm*
  
  ForEach ($doc in $srcfiles) {
  
  $s = $doc.Fullname
  
  Write-Host "Processing :" $doc.FullName
  
  if (stuff) {flag 1 = 1} else {flag 1 = 0}
  if (stuff) {flag 1 = 1} else {flag 1 = 0}
  if (stuff) {flag 1 = 1} else {flag 1 = 0}
  
  $t = "$s;$flag1;$flag2;$flag2"
  
  Write-Host "Output: " $t
  </code></pre>
  
  <p>This all works great. My file processes, the flags are set properly, and a neat semicolon delimited line is generated as $t. However, if I slap these two lines at the end of the function,</p>
  
  <pre><code>$stream = [System.IO.StreamWriter] "flags.txt"
  $stream.WriteLine $t    
  </code></pre>
  
  <p>I get this error. </p>
  
  <pre><code>Unexpected token 't' in expression or statement.
  At C:\CGC003a.ps1:53 char:25
  +     $stream.WriteLine $t &lt;&lt;&lt;&lt;
      + CategoryInfo          : ParserError: (t:String) [], ParseException
      + FullyQualifiedErrorId : UnexpectedToken
  </code></pre>
  
  <p>If I'm reading this write, it appears that <code>write-host</code> flushed my variable <code>$t</code> before it got to the <code>WriteLine</code>. Before I try <code>out-file</code>, though, I want to understand what's happening to <code>$t</code> after Write-Host that prevents <code>Write Line</code> from seeing it as valid. Or is there something else I'm missing? </p>