Question:
I want to add text into specific line of txt file. I can’t find any solution on internet.
This code adds text in further line (I want for example second line):
1 2 3 4 |
$test_out = "test" $test_out | Add-Content "C:\tmp\test.txt" |
Answer:
If you want to add text to the end of a specific line, you can do it like this
1 2 3 4 |
$fileContent = Get-Content $filePath $fileContent[$lineNumber-1] += $textToAdd $fileContent | Set-Content $filePath |
If you want to replace the text instead of adding, just remove the ‘+’ sign.
You do of course have to set the variables
$filePath, $textToAdd and $lineNumber first.