Powershell. Writing out lines based on string within the file

powershell

Solution

Do you want to perform a simple "grep"? Then try this

select-string .\test.txt -pattern "\Athat" | foreach {$_.Line} 

or this (very similar regex), also writes to an outfile

select-string .\test.txt -pattern "^that" | foreach {$_.Line} | out-file -filepath out.txt

This assumes that you want to search for a 4-byte string "that" at the beginning of the string , or beginning of the line, respectively.

Problem

I'm looking for a way to export all lines from within a text file where part of the line matches a certain string. The string is actually the first 4 bytes of the file and I'd like to keep the command to only checking those bytes; not the entire row. I want to write the entire row. How would I go about this? I am using Windows only and don't have the option to use many other tools that might do this. Thanks in advance for any help.

Original source