Find and Replace strings in file

Find and replace 2 (in the example, but could be increased) strings in a file

$FileToSearchIn = "ToBeDefined"
$FileToSearchInBackup = $FileToSearchIn + "-backup"
$StringToSearch1 = "ToBeDefined"
$StringToReplace1 = "ToBeDefined"
$StringToSearch2 = "ToBeDefined"
$StringToReplace2 = "ToBeDefined"

Copy-Item $FileToSearchIn $FileToSearchInBackup
Remove-Item $FileToSearchIn

Get-Content $FileToSearchInBackup | ForEach-Object {
	$_ -replace '$StringToSearch1', '$StringToReplace1' `
	-replace '$StringToSearch2', '$StringToReplace2'
	} | Set-Content $FileToSearchIn

Leave a Reply

Scroll to Top