147

I have several configuration files nested like such:

C:\Projects\Project_1\project1.config
  
C:\Projects\Project_2\project2.config

In my configuration I need to do a string replace like such:

<add key="Environment" value="Dev"/>

will become:

<add key="Environment" value="Demo"/>

I thought about using batch scripting, but there was no good way to do this, and I heard that with PowerShell scripting you can easily perform this. I have found examples of find/replace, but I was hoping for a way that would traverse all folders within my C:\Projects directory and find any files that end with the '.config' extension. When it finds one, I want it to replace my string values.

Any good resources to find out how to do this or any PowerShell gurus that can offer some insight?

2
  • 1
    Let us know how you got on or if there were some odd formatting issues with the files that needed to be addressed. One good thing about the problem is that it's test without affecting production code Commented May 15, 2010 at 1:31
  • Okay, I came here looking for PowerShell too, but I almost got suckered into taking up the "there was no good way to do this [in batch]" challenge. Luckily there are a few impressive cmd.exe/DOS style answers if you look far enough down on this question.
    – ruffin
    Commented Oct 14, 2021 at 20:06

9 Answers 9

217

Here a first attempt at the top of my head.

$configFiles = Get-ChildItem . *.config -rec
foreach ($file in $configFiles)
{
    (Get-Content $file.PSPath) |
    Foreach-Object { $_ -replace "Dev", "Demo" } |
    Set-Content $file.PSPath
}
14
  • 11
    For this to work in files in subdirectories, you need ".PSPath". Interestingly, when I tried to make this work without a () around get-content it failed at write-content because the file was locked. Commented Jun 2, 2010 at 4:00
  • 27
    Short version (common aliases used): ls *.config -rec | %{ $f=$_; (gc $f.PSPath) | %{ $_ -replace "Dev", "Demo" } | sc $f.PSPath }
    – Artyom
    Commented Jul 15, 2013 at 20:21
  • 1
    Using the get-content and set-content inside the same pipeline is giving me an error because the file locked (this seems like the obvious expected behaviour here). I've avoided the issue by storing the get-content result in a variable. Is there something I'm missing that would allow this to work otherwise? Commented Oct 27, 2014 at 23:48
  • 5
    @Artyom don't forget the . after the ls. Got stung by that myself. Commented Sep 26, 2015 at 11:32
  • 7
    UnauthorizedAccessException may also cause due to folders if you will remove the *.config to run on all files. You can add -File filter to the Get-ChildItem... Took a while to figure it out
    – Amir Katz
    Commented May 16, 2017 at 12:00
36

PowerShell is a good choice ;) It is very easy to enumerate files in given directory, read them and process.

The script could look like this:

Get-ChildItem C:\Projects *.config -recurse |
    Foreach-Object {
        $c = ($_ | Get-Content) 
        $c = $c -replace '<add key="Environment" value="Dev"/>','<add key="Environment" value="Demo"/>'
        [IO.File]::WriteAllText($_.FullName, ($c -join "`r`n"))
    }

I split the code to more lines to be readable for you. Note that you could use Set-Content instead of [IO.File]::WriteAllText, but it adds new line at the end. With WriteAllText you can avoid it.

Otherwise the code could look like this: $c | Set-Content $_.FullName.

1
  • With the most voted answer, I was having issues with the files being used by another process (even with the parenthesis around Get-Content) and this solution fixed it. I believe it happened because the way I wrote the script I didn't have access to PSPath (but I am not entirely sure). These files are blocked by p4 and I was calling 'p4 edit' on them before editing. I used the output of Select-String to get the files list first, so I would only call 'p4 edit' on those specific files. Commented Nov 20, 2023 at 19:59
20

This approach works well:

gci C:\Projects *.config -recurse | ForEach {
  (Get-Content $_ | ForEach {$_ -replace "old", "new"}) | Set-Content $_ 
}
  • Change "old" and "new" to their corresponding values (or use variables).
  • Don't forget the parenthesis -- without which you will receive an access error.
1
  • 5
    So I went with this one for succinct expression - but I had to replace Get-Content $_ with Get-Content $_.FullName, and the equivalent for Set-Content for it to handle files that weren't at the root. Commented Sep 28, 2019 at 9:41
16

This powershell example looks for all instances of the string "\foo\" in a folder and its subfolders, replaces "\foo\" with "\bar\" AND DOES NOT REWRITE files that don't contain the string "\foo\" This way you don't destroy the file last update datetime stamps where the string was not found:

Get-ChildItem  -Path C:\YOUR_ROOT_PATH\*.* -recurse 
 | ForEach {If (Get-Content $_.FullName | Select-String -Pattern '\\foo\\') 
           {(Get-Content $_ | ForEach {$_ -replace '\\foo\\', '\bar\'}) | Set-Content $_ }
           }
12

I would go with xml and xpath:

dir C:\Projects\project_*\project*.config -recurse | foreach-object{  
   $wc = [xml](Get-Content $_.fullname)
   $wc.SelectNodes("//add[@key='Environment'][@value='Dev']") | Foreach-Object {$_.value = 'Demo'}  
   $wc.Save($_.fullname)  
}
12

I found @Artyom's comment useful but unfortunately they have not posted it as an answer.

This is the short version, in my opinion a better one, of the accepted answer;

ls *.config -rec | %{$f=$_; (gc $f.PSPath) | %{$_ -replace "Dev", "Demo"} | sc $f.PSPath}
1
  • 3
    In case anyone else runs across this, as I did -- looking to execute this directly from a batch file -- It may help to use foreach-object instead of the % alias when executing a command like this. Otherwise, it may result in the error: Expressions are only allowed as the first element of a pipeline Commented Mar 8, 2018 at 18:50
10

I have written a little helper function to replace text in a file:

function Replace-TextInFile
{
    Param(
        [string]$FilePath,
        [string]$Pattern,
        [string]$Replacement
    )

    [System.IO.File]::WriteAllText(
        $FilePath,
        ([System.IO.File]::ReadAllText($FilePath) -replace $Pattern, $Replacement)
    )
}

Example:

Get-ChildItem . *.config -rec | ForEach-Object { 
    Replace-TextInFile -FilePath $_ -Pattern 'old' -Replacement 'new' 
}
5

When doing recursive replacement, the path and filename need to be included:

Get-ChildItem -Recurse | ForEach {  (Get-Content $_.PSPath | 
ForEach {$ -creplace "old", "new"}) | Set-Content $_.PSPath }

This wil replace all "old" with "new" case-sensitive in all the files of your folders of your current directory.

1
  • 1
    The ".PSPath" part of your answer really helped me. But I had to change the inner "{$" to "$_". I'd edit your answer, but I'm not using your -creplace part--I'm using the accepted answer with .PSPath
    – aaaa bbbb
    Commented Apr 23, 2019 at 20:30
1

Some of the previous answers will convert line endings to Windows-style CRLF regardless whether they were originally UNIX-style LF and may also corrupt files with an unexpected encoding.

The -Replace operator users regular expressions. The .Replace method uses literal text.

(I work with both Windows and UNIX line endings but have standardized to UTF8 without a BOM.)

Specifying the encoding, trapping read errors, and using the -Raw flag to grab all the lines at once (with their delimiters) will mitigate these issues.

$utf8 = New-Object Text.UTF8Encoding
$files = Get-ChildItem -Path 'test1.sql', 'test2.sql'
$find = 'an old needle from our haystack'
$replace = 'a new needle for our haystack'

ForEach ($f In $files) {
   $text_content = $Null
   $text_content = Get-Content -PSPath $f.PSPath -Raw -Encoding utf8 -ErrorAction SilentlyContinue
   If ($text_content) {
      $text_content = $text_content.Replace($find, $replace)
      $byte_content = $utf8.GetBytes($text_content)
      Set-Content -Value $byte_content -Encoding Byte -PSPath $f.PSPath
   }
}
1
  • 1
    Thanks @durette, the original voted answer here worked for me apart from the odd special characters which could get corrupted when the file was written out again, due to encoding issues as you found. So your solution here solved that for me, although I used -replace $find, $replace rather than Replace($find, $replace) as I needed case insensitive matching. Otherwise worked great. Thanks. Commented May 30 at 9:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.