Question: How can I get this script to change the 'Date Modified' ONLY in the files that contain the string that is being replaced? $filePath =
How can I get this script to change the 'Date Modified' ONLY in the files that contain the string that is being replaced?
$filePath = "C:\Users\Desktop\*.bat"
$dateChanged = $false
# Get the files from the folder and iterate using Foreach
Get-ChildItem $filePath -Recurse | ForEach-Object {
$originalContent = Get-Content $_
$modifiedContent = $originalContent -ireplace 'fort\\tester','fort\success'
# Check if the content was modified
if ($originalContent -ne $modifiedContent) {
$modifiedContent | Set-Content $_
# set the $dateChanged = true after the modification of the content
$dateChanged = $true
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
