I need this for myself and below is a slightly better version of the script.
I added the following:
- Detailed parameter support so you can see what changes the script has made.
- Ability to specify folders so that you can limit changes.
- Adding bower.json, txt and md to enable extensions.
- First find and replace the content, rename later.
- Do not replace the contents if the search string is not found (this avoids unnecessary changes on the modified date).
[CmdletBinding(SupportsShouldProcess=$true)] Param() $match = "MyProject" $replacement = Read-Host "Please enter project name" $searchFolders = @("MyProject.JS", "MyProject.WebApi", ".") $extensions = @("*.cs", "*.csproj", "*.sln", "bower.json", "*.txt", "*.md") foreach($searchFolderRelative in $searchFolders) { $searchFolder = join-path (get-location) $searchFolderRelative Write-Verbose "Folder: $searchFolder" $recurse = $searchFolderRelative -ne "." if (test-path $searchFolder) { $files = Get-ChildItem (join-path $searchFolder "*") -file -include $extensions -Recurse:$recurse | Where-Object {Select-String -Path $_.FullName $match -SimpleMatch -Quiet} foreach($file in $files) { Write-Verbose "Replaced $match in $file" ((Get-Content $file.fullname) -creplace $match, $replacement) | set-content $file.fullname } $files = Get-ChildItem $searchFolder -filter *$match* -Recurse:$recurse $files | Sort-Object -Descending -Property { $_.FullName } | % { Write-Verbose "Renamed $_" $newName = $_.name -replace $match, $replacement Rename-Item $_.FullName -newname $newName -force } } else { Write-Warning "Path not found: $searchFolder" } }
Note that one of the responses to the change is that the recurses folder above is only in the specified folders, not the root. If you do not want this, simply set $recurse = true .
Shitalshah
source share