title: Powershell StreamReader ReadLine / ReadToEnd reads same line multiple times
body: <p>I have a function to retrieve the last n lines of a text file (in this case a log file):</p>
  
  <pre><code>function Get-Tail([object]$reader, [int]$count = 10) {
  
      $lineCount = 0
      [long]$pos = $reader.BaseStream.Length - 1
  
      while($pos -gt 0)
      {    
          $reader.BaseStream.position=$pos
  
          if ($reader.BaseStream.ReadByte() -eq 10)
          {
              $lineCount++
              if ($lineCount -ge $count) { break }
          } 
          $pos--
      } 
  
      # tests for file shorter than requested tail
      if ($lineCount -lt $count -or $pos -ge $reader.BaseStream.Length - 1) {
          $reader.BaseStream.Position=0
      } else {
          $reader.BaseStream.Position = $pos+1
      }
  
      # debug
      write-host $reader.readtoend()
  
      [....]
  }
  </code></pre>
  
  <p>Trapping at Write-Host I find that $pos is 166 (i.e. well before EndOfStream). </p>
  
  <p>The output from this is:</p>
  
  <pre><code>2013/02/23 03:39:13 ERROR 2 (0x00000002) Accessing Source Directory \\[redacted]
  The system cannot find the file specified. -------------------------------------------------------------------------------
  
    Started : Sat Feb 23 03:39:13 2013
  
     Source : [redacted]
       Dest : [redacted]
  
      Files : *.*
  
    Options : *.* /FFT /V /TS /FP /NDL /TEE /S /E /COPY:DATS /SECFIX /PURGE /MIR /B /NP /XO /XN /XC /R:0 /W:0 
  
  ------------------------------------------------------------------------------
  
  2013/02/23 03:39:13 ERROR 2 (0x00000002) Accessing Source Directory \\[redacted]
  The system cannot find the file specified. 
  </code></pre>
  
  <p>So, the last two full lines are repeated. I have tried flushing the streamreader first, makes no odds.</p>
  
  <p>This is a bit confusing. I assume I am making a schoolboy error, but can't work out which one. Any ideas please?</p>