Home > Uncategorized > Bulk Rename Operation in TFS using Powershell

Bulk Rename Operation in TFS using Powershell

If the structure of your TFS source control requires an overhaul this may help.

Lift With Your LegsFirst if you haven’t already, install the TFS Power Tools. Make sure you find the most recent release.

Here is a link to the most recent version to date: http://visualstudiogallery.msdn.microsoft.com/c255a1e4-04ba-4f68-8f4e-cd473d6b971f

During installation be sure to install the TFS Cmdlets for Powershell.

If you’ve never used Powershell before fire up a PS Console from the shortcut found under TFS Power Tools in your program menu.

Then set the execution policy by typing the following:

Set-ExecutionPolicy RemoteSigned

OK. Now you’re ready to do some damage. 

SERIOUSLY careful with this!

You don’t want to mess up your repository any more than it already is.
The following snippet is just an example of something you could do, but is not meant to be used as is.  You will need to modify it to suit your requirements.

In my situation I had created about 25+ separate directories for different website projects, each with their own set of branches, Dev, Main, etc.  It looked something like this:

WebSite1/Dev/…
WebSite2/Dev/…
WebSite3/Dev/…

and so on.  I realized I would have to create an additional directory under “Dev” which was the root of the branch to contain each website.   It would have to be modified to look more like this:

WebSite1/Dev/WebSite/…
WebSite2/Dev/WebSite/…
WebSite3/Dev/WebSite/…

So, first I tried to use the Source Control Explorer “Move” command through the GUI.  That didn’t work too well. SCE didn’t seem to want me to move all the files at once in a drag and drop operation and I didn’t want to have to painstakingly drag each directory individually.

Powershell to the rescue…

The Powershell CmdLets for TFS installed with the Power Tools include one item called:

Get-TfsChildItem

This allows you to recursively enumerate source controlled items.

Try this — Don’t worry it’s safe:

Get-TfsChildItem $/YourProject/*  -Recurse

So, from there we can get the server paths of everything in source control. Combine them with some Where-Object, Select-Object and foreach in Powershell along with the TFS command line utility, TF.exe and we have the makings of a solution.

$sites = “WebSite1”, “WebSite2”, “WebSite3”

foreach($item in $sites){

tf get $/MyProject/$item/Dev/

write-host “Renaming directories”

Get-TfsChildItem $/MyProject/$item/Dev/* -Folder | Where-Object { -not $_.ServerItem.Contains(“/WebSite”)  }| Select-Object ServerItem | foreach { tf rename $_.ServerItem $_.ServerItem.Replace(“/Dev/”,”/Dev/WebSite/” ) }

Write-Host “About to checkin”

tf checkin $/MyProject/$item/Dev/ /recursive /noprompt

Write-Host “Renaming root items”

Get-TfsChildItem $/MyProject/$item/Dev/*.*  -Recurse | Where-Object { -not $_.ServerItem.Contains(“/WebSite”)  }| Select-Object ServerItem | foreach { tf rename $_.ServerItem $_.ServerItem.Replace(“/Dev/”,”/Dev/WebSite” ) }

Write-Host “About to checkin”

tf checkin $/MyProject/$item/Dev/ /recursive /noprompt
}

Advertisements
Categories: Uncategorized
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: