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=18.104.22.168, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" Add-Type -AssemblyName "Microsoft.TeamFoundation.WorkItemTracking.Client, Version=22.214.171.124, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
Tip: if you don’t know the Public Key Token, you can find out by using “sn.exe”:
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