find row text in files

Hi All,
I am trying to find rows in test files that contain the text “H: \”. If match write the row(s) that contain “H: \” to out-file

I tried the following but not having much luck, no results. Please help > Thanks in advance
$Path = “C:\files”

Get all files in $Path that end in “.txt”.

Get-ChildItem $Path -Filter “*.txt” |
ForEach-Object {
(Get-Content $_.FullName | Select-String “H: \”) | out-file c:\coll1.txt


Hey Mate,

This should work fine for you.

 Get-ChildItem C:\Folder\*.txt -Recurse | Select-String 'H: \\' | Export-Csv C:\Matchinglines.csv -nti 

-Recurse allows us to go through all of the files and folders in this directory. In this case we just simply grab any file with a .txt extension. Select-String allows us to specify what we want to find within our text files. After we’ve gathered that we Export that to a CSV.

As with anything in PowerShell there are a million ways to do things, but hopefully this helps.

Edit: as Curtis said, make sure you know what you’re searching for as \ is the escape character in regex.

your code does work, but have some clarification questions.

  1. Do you actually expect the space between the : and the first ?
  2. Are you actually wanting to test for two “” or just one? Search-String uses RegEx match patterns, so a match pattern of “H: \” will match "H: ". This is because “” is a speacial metacharacter. If you literally want two “” then your search pattern should be “H: \\”

and lastly, in general, you don’t need to find all the txt files first using get-childitem. Get-Content can use wildcards in the path parameter

$Path = "C:\files"

# Get all files in $Path that end in ".txt".
Get-Content -Path "$Path\*.txt" | Select-String "H: \\\\" | Out-File c:\coll1.txt

Thank you both for the great tips!
yep, the spaces throw me for a loop :slight_smile: it works now after i added //// Thanks again!

My apologies I meant to say Curtis