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

3
  • 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. Commented Jun 21, 2021 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. Commented Jun 21, 2021 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 Commented Jun 22, 2021 at 6:06

1 Answer 1

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'
Sign up to request clarification or add additional context in comments.

6 Comments

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?
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 you're right, the Join-Path is redundant, I'll update my answer.
Almost. Now it's missing only the filename. Filename - batch3 ..........................
@tinman updated once more, it should be $_.Filename not $_.Name. Like this: -f $_.Filename, $_.Line
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.