user1794679
user1794679

Reputation: 33

Add-Content with a Select-String as the added content?

My intention is to look within a text file (C:\Users\pmaddox\Desktop\VaughnResers eligibility text.txt) and find the specific date and enter the entire line that contained said date into a second file (VaughnReserBatTest.txt). I hoped to accomplish this by using Add-Content's format (location "input") with Select-String's piped Select-Object Line to bring the raw string from the text line into the Add-Content cmdlet but thus far I am having now luck...

My current script looks like this:

Add-Content \\Waqmed1\pmaddox$\VaughnReserBatTest.txt Select-String  C:\Users\pmaddox\Desktop\VaughnResers eligibility text.txt  -pattern "10/11/1988" | Select-Object Line

I am fairly new to powershell and I believe my answer lies somewhere in either the "create and object" direction or the "fix your damn code" direction. Any help would be appreciated!

Upvotes: 3

Views: 4559

Answers (2)

CB.
CB.

Reputation: 60976

Try

get-contentc "C:\Users\pmaddox\Desktop\VaughnResers eligibility text.txt" |
select-string -Pattern '10/11/1988' ) | select -expa line | 
add-content \\Waqmed1\pmaddox$\VaughnReserBatTest.txt 

Upvotes: 2

dugas
dugas

Reputation: 12493

Assume the file that contains the desired line is named a.txt and the file you want to write the desired line to is named b.txt:

Get-Content .\a.txt | Select-String "10/11/1988" | Add-Content .\b.txt

Note that all matching lines frorm a.txt will be written to b.txt, so if there is more than one match then you will write multiple entries to b.txt. If you only want the first match, you can use the following:

Get-Content .\a.txt | Select-String "10/11/1988"  | Select-Object -First 1 | Add-Content .\b.txt

Upvotes: 1

Related Questions