Replace contents only if whole line matches

I currently have the following code that someone else provided and it works for what it was intended, but I would like to modify it so that when it finds the search string, that the whole line must match.

Basically there are configuration files that have the following line and most of the time that is the extent the line. ECPEXT=

I then want to replace the line so that it becomes: ECPEXT=ps1

Occasionally the line will have something else and I would like to avoid modifying those since they are specific to a different process. I would also like to avoid if/when the process runs against the same configuration file and the line becomes: EXPEXT=ps1ps1 or EXPEXT=ps1pl

foreach ($file in gci -Recurse $filespec | ? { Select-String $search $_ -Quiet } ) { (gc $file) | ForEach-Object {$_ -replace $search, $replace } | Set-Content $file } 

submitted by /u/Snassek
[link] [comments]

Leave a Reply