I am using a modified version of the code in this post.
$fileToCheck = "c:\Users\me\desktop\test.txt"
$fileContents = Get-Content $fileToCheck
$line = $fileContents | Select-String "hosts" | Select-Object -ExpandProperty Line
if ($line -eq $null) {
"String Not Found"
exit
}
echo $line
$modifiedContents = $fileContents | ForEach-Object {$_ -replace $line,'hey'}
echo $modifiedContents
This is my test.txt
file's contents:
X.Y:
hosts: ["https://localhost:111"]
There are 2 whitespaces preceding hosts
.
I am unable to get the line to be replaced. I've narrowed it down to the fact that it is probably due to one of the special characters that are being used in that line, which is causing the issue.
I've tried the modified code with the original data and everything works.
Not sure what gives in my case?
Thank you so much for your time.