Copy and Paste the below content in Set-FindandReplaceString.PS1. Please read the instructions carefully. Try to do a dry run on some test folders for better results.
#############################################################################
# Description : Find and Replace string on any local or remote machine
# Input : Be specific with your string.Its a recursive update. Please be very careful.
# Parameters : ServerName (Server01)
# DriveLetter ( F or G or H)
# Extension (PS1, DOC, SQL etc:-)
# SearchString (ABC)
# ReplaceString (CBA)
# SearchFolder /*Note – Give full path of folder excluding the drive letter as the drive letter is already been
# given in a second parameter */
# If SearchFolder Input is No , It will traverse through entire drive
# If SearchFolder Input is Yes , Give the folder path for example, search abc string under f:\SQL\Temp folder,Enter just SQL\Temp
#
#
# Set-FindandReplaceString $server $drive $extn $searchString $replaceString $SearchinFolder
# Output : All Files will be updated with a given pattern
################################################################
Function Set-FindandReplaceString
{
Param([String]$server,[char]$drive,[String]$extn,[String]$searchstring,[String]$replacestring, [String]$Folder)
IF ($Folder -eq ‘No’)
{
$scriptFiles=Get-ChildItem \\$server\$drive$ *.$extn -recurse -force -ErrorAction SilentlyContinue
$ScriptFiles
foreach ($file in $scriptFiles)
{
(Get-Content $file.PSPath) | Foreach-Object {$_ -replace $searchstring, $replacestring} | Set-Content $file.PSPath
}
}
elseif($folder -eq ‘Yes’)
{
$FolderStruncture = Read-Host ” Enter the Folder Name”
if(test-path \\$server\$drive$\$FolderStruncture -pathtype container)
{
$scriptFiles=Get-ChildItem \\$server\$drive$\$FolderStruncture *.$extn -recurse -Force -ErrorAction SilentlyContinue
$ScriptFiles
foreach ($file in $scriptFiles)
{
(Get-Content $file.PSPath) | Foreach-Object {$_ -replace $searchstring, $replacestring} | Set-Content $file.PSPath
}
}
else
{
write-host “Invalid directory Structure”
}
}
else
{
Write-host “Enter the correct option”
}
}
$server=Read-Host ” Enter the servername”
$drive = Read-Host ” Enter the DriveName”
$extn = Read-Host ” Enter the extn”
$searchstring=Read-Host ” Enter the pattern to search”
$replacestring=Read-Host ” Enter the pattern to search”
$SearchinFolder = Read-Host ” Do you need to Search the pattern in Folder (No/Yes)”
Set-FindandReplaceString $server $drive $extn $searchString $replaceString $SearchinFolder