Skip to content

Instantly share code, notes, and snippets.

@Daggerpov
Created December 29, 2025 18:05
Show Gist options
  • Select an option

  • Save Daggerpov/803bbee2cb7dce48cbe4846a6ce143c1 to your computer and use it in GitHub Desktop.

Select an option

Save Daggerpov/803bbee2cb7dce48cbe4846a6ce143c1 to your computer and use it in GitHub Desktop.
Replace all Guids in a File
## GuidSwap.ps1
##
## Reads a file, finds any GUIDs in the file, and swaps them for a NewGUID
##
# Paste whichever file you'd like to execute this script for
# IMPORTANT.
$filename = "{FILEPATH HERE}"
$text = [string]::join([environment]::newline, (get-content -path $filename))
$sbNew = new-object system.text.stringBuilder
$pattern = "[a-fA-F0-9]{8}-([a-fA-F0-9]{4}-){3}[a-fA-F0-9]{12}"
$lastStart = 0
$null = ([regex]::matches($text, $pattern) | % {
$sbNew.Append($text.Substring($lastStart, $_.Index - $lastStart))
$guid = [system.guid]::newguid()
$sbNew.Append($guid)
$lastStart = $_.Index + $_.Length
})
$null = $sbNew.Append($text.Substring($lastStart))
$sbNew.ToString() | out-file -encoding ASCII $filename
Write-Output "Done"
@Daggerpov
Copy link
Author

Inspiration: https://stackoverflow.com/questions/2201740/replacing-all-guids-in-a-file-with-new-guids-from-the-command-line/2283754#2283754

Powershell command on Windows to run: powershell -noexit C:\Users\{filepath of the above script}.ps1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment