title: Search large .log for specific string quickly without streamreader
body: <p>Problem: I need to search a large log file that is currently being used by another process. I cannot stop this other process or put a lock on the .log file. I need to quickly search this file, and I can't read it all into memory. I get that StreamReader() is the fastest, but I can't figure out how to avoid it attempting to grab a lock on the file.</p>
  
  <pre><code>$p = "Seachterm:Search"
  $files = "\\remoteserver\c\temp\tryingtofigurethisout.log"
  
  $SearchResult= Get-Content -Path $files | Where-Object { $_ -eq $p }  
  </code></pre>
  
  <p>The below doesn't work because I can't get a lock of the file. </p>
  
  <pre><code>$reader = New-Object System.IO.StreamReader($files)
  
  $lines = @()
  
  if ($reader -ne $null) {
      while (!$reader.EndOfStream) {
          $line = $reader.ReadLine()
          if ($line.Contains($p)) {
              $lines += $line
          }
      }
  }
  
  $lines | Select-Object -Last 1
  </code></pre>
  
  <p>This takes too long: </p>
  
  <pre><code>get-content $files -ReadCount 500 |
   foreach { $_ -match $p }
  </code></pre>
  
  <p>I would greatly appreciate any pointers in how I can go about quickly and efficiently (memory wise) searching a large log file.</p>