- A PowerShell function to replace things and use Git in the process.
- Driver code originated from https://mcpmag.com/articles/2018/08/08/replace-text-with-powershell.aspx
- Requested by OMG member
Created
August 29, 2024 04:17
-
-
Save tomodachi94/39465c2895f366c10fde2fdfc4542db2 to your computer and use it in GitHub Desktop.
batch-replace.ps1
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/pwsh | |
# see ./README.md for more information | |
function Replace-Safely { | |
[CmdletBinding()] | |
param( | |
[Parameter()] | |
[string] $old | |
[string] $new | |
[string] $path | |
) | |
$path = Get-ChildItem -Path Env:\Script-TargetReplaceFile | |
cd $path | |
git add $path | |
git commit -m "Automatic commit before processing" # if no changes, the commit attempt will be ignored | |
((Get-Content -path $path -Raw) -replace $old,$new) | Set-Content -Path $path # Replaces all occurances of "foo" with "bar" | |
git add $path | |
git commit -m 'Replace ' $old ' with ' $new | |
} | |
Replace-Safely -old foo -new bar -path C:\AbsolutePath\To\Notes.md |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment