Powershell search text file for match and add carriage return at the end of the line

You can achieve this in a single pipeline:

Get-Content -Path "C:\abc.txt" | foreach {
   if ($_ -like "gh*") {
       "" # additional empty line
} | Out-File "C:\abc.txt" 

Instead of inserting any line breaks or carriage returns, outputting an empty line (empty string) is enough. The line breaks will be automatically handled by Out-File.

Instead of -like "gh*" (to satisfy the requirement of your example), you can of course do -match <your regex> or whatever you like.

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top