2

I'm trying to get a specific string from a text file and output it and the filename to a separate txt file. I have tried the following but get an error message. I've looked for answers but haven't found any. Any help is appreciated. I Should add that I'm fairly new to Powershell.

Select-String -Path C:\temp\test1.txt -Pattern 'batch3'|ForEach-Object {   
@' File name - {0} {1}
 ..................... '@ -f $_.Name, (Get-Content $_.FullName -Raw) }
| Out-File 'C:\temp\test_output.txt'

It works if I substitute Select-String for Get-Content. The problem then is that it takes the entire content of the file and that is not what I need.

error message:

Get-Content : Cannot bind argument to parameter 'Path' because it is null. At line:6 char:29 + '@ -f $.Name, (Get-Content $.FullName -Raw) + ~~~~~~~~~~~ + CategoryInfo : InvalidData: (:) [Get-Content], ParameterBindingValidationException + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.GetContentCommand

tinman
  • 21
  • 1
  • 1
    What is your expected output for `test_output.txt` ? I assume you're testing this to then find all files were you're pattern exists. By the way, a Here-String must end being the first character of the new line. – Santiago Squarzon Jun 21 '21 at 14:25
  • I agree that it's not clear what you're intending to put in the output file. It seems to me like you're using the wrong parameter names for the format operator. You can see what parameters Select-String makes available as an example by running: `Select-String -Path C:\temp\test1.txt -Pattern 'batch3' | Select-Object -Property * -First 1`. I'm guessing you want Path and Line but it's not clear. – Bacon Bits Jun 21 '21 at 16:49
  • I apologise. The purpose is to get specific text extracted from many files in a folder and output that into a text file. I also need the name of the file either as the name or included in the file next to the text. If that makes any sense? Something like this: `NameOfFile batch3 NameofFile2 batch3 NameofFile3 batch3` – tinman Jun 22 '21 at 06:06

1 Answers1

1

Select-String does not output a property FullName. However, there is Path property. Try this:

(Get-Content $_.Path -Raw)

This will fix the error, but if you want to output just a line with the string you found, not the entire file contents, remove Get-Content and try this:

Select-String -Path C:\temp\test1.txt -Pattern 'batch3'|ForEach-Object {   
@' File name - {0} {1}
 ..................... '@ -f $_.Filename, $_.Line }
| Out-File 'C:\temp\test_output.txt'
Michal Rosenbaum
  • 1,801
  • 1
  • 10
  • 18
  • I noticed that using `Get-Content` instead of `Select-String` alomst does what I want. It outputs all the contents of the file instead. Maybe there is a way of using that method instead? – tinman Jun 22 '21 at 06:09
  • I tried your method and got this: `Get-Content : Cannot find path 'C:\temp\test1.txt\test1.txt' because it does not exist. At line:6 char:17 + '@ -f $_.Name, (Get-Content (Join-Path $_.Path $_.Filename) -Raw) + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : ObjectNotFound: (C:\temp\test1.txt\test1.txt:String) [Get-Content], ItemNotFoundException + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetContentCommand` The output looks like this: `File name - .....................` – tinman Jun 22 '21 at 08:02
  • @tinman you're right, the `Join-Path` is redundant, I'll update my answer. – Michal Rosenbaum Jun 22 '21 at 09:09
  • Almost. Now it's missing only the filename. `Filename - batch3 ..........................` – tinman Jun 22 '21 at 09:28
  • @tinman updated once more, it should be `$_.Filename` not `$_.Name`. Like this: `-f $_.Filename, $_.Line` – Michal Rosenbaum Jun 22 '21 at 10:30
  • Thank you so much. This works. Now I just have to figure out how to have it go though a folder recursively, filled with these files. – tinman Jun 22 '21 at 11:02