PowerSQL – Find and Replace string on multiple file of local or remote machine

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

Advertisements

About Prashanth Jayaram

I’m a Database technologist having 11+ years of rich, hands-on experience on Database technologies. I am Microsoft Certified Professional and backed with a Degree in Master of Computer Application. My expertise lies in T-SQL programming, Replication and PowerShell You can connect me via https://social.technet.microsoft.com/profile/prashanth jayaram/ http://www.sqlshack.com/author/prashanth/ https://www.tumblr.com/blog/prashantjayaram http://www.sqlservercentral.com/blogs/powersql-by-prashanth-jayaram/ http://www.toadworld.com/members/prashanthjayaram/ My Articles are published in following sites http://www.ssas-info.com/analysis-services-articles/ http://db-pub.com/ http://www.sswug.org/sswugresearch/community/
This entry was posted in PowerShell. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s