Bulk update TFS work items using PowerShell

Today I was faced with a question that persuaded me to try a bit of technology I haven’t used too often yet. I had to set a specific field to a specific value for a large amount of work items. Of course there are multiple ways to achieve that, like using Excel or writing a little application using the TFS API. I decided to brush up on my PowerShell skills and write a script for this purpose.

One of the cool things about PowerShell is that it lets you load any .Net assembly by using the Add-Type cmdlet. You can then use the classes defined in that assembly inside your PowerShell session. This opens up a wide range of possibilities, one of them being that you can use the TFS API from PowerShell!

So, let’s start by loading the TFS assemblies for working with the TFS Work Item store. These are the assemblies for TFS 2013:

Add-Type -AssemblyName "Microsoft.TeamFoundation.Client, Version=12.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
Add-Type -AssemblyName "Microsoft.TeamFoundation.WorkItemTracking.Client, Version=12.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"

Tip: if you don’t know the Public Key Token, you can find out by using “sn.exe”:

image

After the TFS assemblies have been loaded, we can get a reference to the TPC:

$tfs = [Microsoft.TeamFoundation.Client.TfsTeamProjectCollectionFactory]::GetTeamProjectCollection($tfsUri)

And then the $tfs object can be used to access the required services (the work item store in this case):

$workItemStore = $tfs.GetService([Microsoft.TeamFoundation.WorkItemTracking.Client.WorkItemStore])

From then on, the rest is easy and pretty much similar to using the API as you would from C#. You can find the entire script on my GitHub: TfsBulkUpdateWi.ps1

Happy PowerShell-ing!

Advertisements

2 thoughts on “Bulk update TFS work items using PowerShell

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