I check a file for lines where a char in a special position is missing. If the char is missing, I insert it in that exact location. Now I want to write that new line with that extra char over the old line into the file. For example I have the following in the file:
"C:\\path\test\test"
"C:\\path\test\test2\"
"C:\\path\test\test3\"
After I run my short script, I want it to look like:
"C:\path\test\test\"
"C:\path\test\test2\"
"C:\path\test\test3\"
I try to accomplish that using this little loop:
$content = Get-Content C:\path\to\file.txt
foreach ($line in $content)
{
if($line[-25] -ne '\') {
$test = $line -replace '(?<!\\)(?=.{24}$)', '\' | Set-Content C:\path\to\file.txt
}
else {
continue
}
}
That code does insert me the char at the correct position. But in the end only line one is in the file, instead of all three lines. I think I am close, but just started with powershell.