Find And Replace Values In All Files With Powershell

Pavol Kutaj
1 min readSep 30, 2021

The aim of this page📝 is to write a short PowerShell snippet finding and replacing values in all markdown files within a folder/subfolders.

  • this is for all markdown files (*.md)
  • get a .FullName property after a folder is filtered with dir command
  • iterate over the collection with ForEach-Object loop structure
  • use -Raw for Get-Content ~> Get-Content without the parameter -Raw produces an array of strings (one string per line), this would be messy with Set-Content without newlines param (see below)
  • use -NoNewLine for Set-Content ~> version control will not be finding newlines at the end
function find_and_replace($find, $replace) {
(dir *.md -Recurse).FullName | ForEach-Object {
(Get-Content -Path $_ -Raw) -replace "$find", "$replace" |
Set-Content -Path $_ -NoNewline }
}

2. links

--

--

Pavol Kutaj

Today I Learnt | Infrastructure Support Engineer at snowplow.io with a passion for cloud infrastructure/terraform/python/docs. More at https://pavol.kutaj.com