I modified your script to make it easier for me to test and troubleshoot :
$TextFiles = @('C:\Test.txt',
'C:\Test2.txt'
)
foreach ($FilePath in $TextFiles) {
Write-Output "Searching for string in $FilePath"
$Content = Get-Content $FilePath
if ($Content -match 'mdt-01') {
Write-Output "Found mdt-01 match in $FilePath"
Write-Output "`$Content is : `r`n $Content"
$NewContent = $Content | % { $_.Replace('mdt-01', 'replacethis') }
Write-Output "And after the Replace for mdt-01, `$NewContent is now : `r`n $NewContent"
# Set-Content -Path $FilePath -Value $NewContent
}
if ($Content -match 'MDT-01') {
Write-Output "`$Content is : `r`n $Content"
$Content = $Content | % { $_.Replace('MDT-01', 'replacethis') }
Write-Output "And after the Replace for MDT01, `$Content is now : `r`n $Content"
# Set-Content -Path $FilePath -Value $Content
}
}
Here is the ouput :
[blockquote]Searching for string in C:\Test.txt
Found mdt-01 match in C:\Test.txt
$Content is :
WSUSServer=http://mdt-01:8530 blablabla WSUSServer=http://MDT-01:8530
And after the Replace for mdt-01, $NewContent is now :
WSUSServer=http://replacethis:8530 blablabla WSUSServer=http://MDT-01:8530
$Content is :
WSUSServer=http://mdt-01:8530 blablabla WSUSServer=http://MDT-01:8530
And after the Replace for MDT01, $Content is now :
WSUSServer=http://mdt-01:8530 blablabla WSUSServer=http://replacethis:8530
Searching for string in C:\Test2.txt
Found mdt-01 match in C:\Test2.txt
$Content is :
WSUSServer=http://mdt-01:8530 blablabla Test2 WSUSServer=http://MDT-01:8530
And after the Replace for mdt-01, $NewContent is now :
WSUSServer=http://replacethis:8530 blablabla Test2 WSUSServer=http://MDT-01:8530
$Content is :
WSUSServer=http://mdt-01:8530 blablabla Test2 WSUSServer=http://MDT-01:8530
And after the Replace for MDT01, $Content is now :
WSUSServer=http://mdt-01:8530 blablabla Test2 WSUSServer=http://replacethis:8530
[/blockquote]
So, we can see that the string “mdt-01” is indeed replaced.
But at the beginning of the second If statement ( the one looking for “MDT-01”), we see that $Content is back to its original value.
Your first change (the replacement of “mdt-01” by “replacethis”) has been discarded.
This is because you stored this change in the variable $NewContent, and then, at the beginning of the second If statement, you go back to using $Content.
So, if we stop using $NewContent and work only with $Content, like so :
$TextFiles = @('C:\Test.txt',
'C:\Test2.txt'
)
foreach ($FilePath in $TextFiles) {
Write-Output "Searching for string in $FilePath"
$Content = Get-Content $FilePath
if ($Content -match 'mdt-01') {
Write-Output "Found mdt-01 match in $FilePath"
Write-Output "`$Content is : `r`n $Content"
$Content = $Content | % { $_.Replace('mdt-01', 'replacethis') }
Write-Output "And after the Replace for mdt-01, `$Content is now : `r`n $Content"
# Set-Content -Path $FilePath -Value $NewContent
}
if ($Content -match 'MDT-01') {
Write-Output "`$Content is : `r`n $Content"
$Content = $Content | % { $_.Replace('MDT-01', 'replacethis') }
Write-Output "And after the Replace for MDT01, `$Content is now : `r`n $Content"
# Set-Content -Path $FilePath -Value $Content
}
}
Now, the first change is kept and both ‘mdt-01’ and ‘MDT-01’ are replaced in the final output :
[blockquote]Searching for string in C:\Test.txt
Found mdt-01 match in C:\Test.txt
$Content is :
WSUSServer=http://mdt-01:8530 blablabla WSUSServer=http://MDT-01:8530
And after the Replace for mdt-01, $Content is now :
WSUSServer=http://replacethis:8530 blablabla WSUSServer=http://MDT-01:8530
$Content is :
WSUSServer=http://replacethis:8530 blablabla WSUSServer=http://MDT-01:8530
And after the Replace for MDT01, $Content is now :
WSUSServer=http://replacethis:8530 blablabla WSUSServer=http://replacethis:8530
Searching for string in C:\Test2.txt
Found mdt-01 match in C:\Test2.txt
$Content is :
WSUSServer=http://mdt-01:8530 blablabla Test2 WSUSServer=http://MDT-01:8530
And after the Replace for mdt-01, $Content is now :
WSUSServer=http://replacethis:8530 blablabla Test2 WSUSServer=http://MDT-01:8530
$Content is :
WSUSServer=http://replacethis:8530 blablabla Test2 WSUSServer=http://MDT-01:8530
And after the Replace for MDT01, $Content is now :
WSUSServer=http://replacethis:8530 blablabla Test2 WSUSServer=http://replacethis:8530
[/blockquote]