7 Nov

Massive Search & Replace Among Files Checked-in to TFS

Category:General PostTag: :

This post is a note to myself, as the two outlined below contain code/commands I?ve done (wished I could have done automatically in the past) that I want to save for reference later.

I spent the last (almost full year) taking baby steps in an effort to make our logic layer a tiny smidgen eency weency bit more testable. Trust me, trying to replace an everything?s a Singleton architecture is no easy task. I won?t go into my strong dislike for Singletons ? take a look at Singletons are Evil. I?ve slowly worn the team down into an agreement that we will not propagate any more Singletons in the project.  They?ve given me a new saying ?Read my lips. NO NEW SINGLETONS!?.

    PowerShell assisted regular expression search and replace.

    In my case, the specific regular expression needed to find


// and replace it with

Below is the PowerShell script I used.

# define the find and replacement regular expressions.

$regex_find = ‘([a-zA-Z]+)\.Instance\.’

$regex_replace = ‘Resolve<I$1>().’

# get all the C# files we want to search/replace through

$allFiles = Get-ChildItem -Filter *.cs -Recurse

foreach($fileToReplace in $allFiles)


    $fileString = $fileToReplace.FullName

    if([String]::Join([Environment]::NewLine, (Get-Content -Path $fileString)) -match $regex_find)


        # make the file writable so we can update it

        $fileToReplace.IsReadOnly = $false;

        Write-Host "applying fix to – $fileString"

        # here?s where we load up the file iterate through each line   
        # replace any of the regex matches

        # and save the file back to the original location

        Set-Content $fileString -Value ((Get-Content -Path $fileString) | foreach { if( $_ -match $regex_find) { $_ -replace $regex_find, $regex_replace } else { $_ } } )





  • Some of the files ?end of file? changed (added an extra end line in some cases)


Team Foundation Server tip to detect the changed files.


The next step requires you install the TFS Power Tools.

Once installed, fire up the PowerShell console given in the start menu


I then ran the following command to have all edited files automatically detected as modified and can then be set as pending a change in TFS.

tfpt online /adds /diff /recursive .

5 thoughts on “Massive Search & Replace Among Files Checked-in to TFS

  1. A. This is cool. Being able to work on the code in the repo is pretty slick.
    B. I wonder if this is really risky, though.

    Just thinking through another scenario, would it be safer to pull all the code, do the search and Replace within Visual Studio, and then ensure I get all my changes to pass tests before I commit them?

  2. Is the IoC anti-pattern (i.e. coupling to the container, rather than dependency injection) much of an improvement on using singletons?

  3. @David Starr

    A. Not sure, what you mean by “in the repo”. I’m still working on my local dev disk. So I’m not changing live in the repo. Which is the purpose of using tfs power tools to detect what changed locally and queue it up to be checked in. This gives me the opportunity to diff each file that actually changed.

    B. I’m sure there are risks here, luckily I didn’t bump into any.

  4. @Graham
    Yes – but as Singletons aren’t the main topic of the post, and the context required to explain the reasoning for the search/replace is way out of scope. I’ll just leave it at, yes it is an improvement.

Comments are closed.