Skip to content

Instantly share code, notes, and snippets.

@Himura2la
Last active March 5, 2021 13:18
Show Gist options
  • Save Himura2la/c76e7f4add7280aaa4bfd60fa4472782 to your computer and use it in GitHub Desktop.
Save Himura2la/c76e7f4add7280aaa4bfd60fa4472782 to your computer and use it in GitHub Desktop.
Replace all occurrences of a string in a directory
function MassReplace {
param (
[Parameter(Mandatory=$true)] [string] $Path,
[Parameter(Mandatory=$true)] [string] $Include,
[Parameter(Mandatory=$true)] [string] $FindString,
[Parameter(Mandatory=$true)] [string] $ReplaceString
)
Get-ChildItem $Path -Include $Include -Recurse
| Select-String $FindString -SimpleMatch
| Group-Object -Property Path
| ForEach-Object {
(Get-Content -LiteralPath $_.Name -Raw).Replace($FindString, $ReplaceString)
| Set-Content -NoNewline -LiteralPath $_.Name
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment