Quick Response Sample – Command line utility to manage Team Foundation Server Teams and Users

Quick Response Sample – Command line utility to manage Team Foundation Server Teams and Users

Rate This
  • Comments 4

Quick Response samples provide information directly from Visual Studio ALM Rangers working with the Microsoft Visual Studio Product Group, in response to feature gaps to supplement the product and knowledge base information.



The Visual Studio and Team Foundation Server VM Factory automation team, working on the Visual Studio Readiness “Gig” Hands-on Lab (HOL) prerequisite setup automation, required a command line utility, such as TFSSecurity.exe , to add Teams and Team members to a Team Project. Investigations highlighted that in Visual Studio Team Foundation Server 2012 RC, a command line utility with the feature to manage teams and their members does not exist. A feature gap !


Team Foundation Server has extensive support for Teams and Team membership management through the object model. You can build your own Teams administrative command line utility, using the ALM Rangers oob_TfsTeams sample solution, which you can download from Supporting Guidance and Whitepapers, in the Quick Response download package.


The sample is based on using the Team service which an extension on top of the Identity Management Service (IMS) in TFS. Refer to Team Foundation Server Architecture for more information on these services.

To use the sample, proceed as follows:

  • Rebuild the sample code using Visual Studio 2012 RC (or higher), whereby you require the TFS Object Model (OM).
  • Run the TFSTeams.exe command line tool with the /? parameter to get parameter help.
  • Run the TFSTeams.exe command line tool to list or create teams, and to add members to teams.
  • [Optionally] extend the sample code to implement other administrative features.

Sample extract, TeamWrapper.cs:

 1: // Copyright © Microsoft Corporation. All Rights Reserved.
 2: // This code released under the terms of the 
 3: // Microsoft Public License (MS-PL, http://opensource.org/licenses/ms-pl.html.)
 4: // This is sample code only, do not use in production environments
 5: namespace Microsoft.ALMRangers.TfsTeams
 6: {
 7:     using System;
 8:     using System.Collections.Generic;
 9:     using System.Linq;
 10:     using Microsoft.TeamFoundation.Client;
 11:     using Microsoft.TeamFoundation.Framework.Client;
 12:     using Microsoft.TeamFoundation.Framework.Common;
 13:     using Microsoft.TeamFoundation.Server;
 14:     using System.Diagnostics;
 16:     internal class TeamWrapper : IDisposable
 17:     {
 18:         private readonly TfsTeamProjectCollection teamProjectCollection;
 19:         private readonly TfsTeamService teamService;
 20:         private readonly ProjectInfo projectInfo;
 21:         private readonly IIdentityManagementService identityManagementService;
 23:         public TeamWrapper(Uri collectionUri, string teamProjectName)
 24:         {
 25:             this.teamProjectCollection = new TfsTeamProjectCollection(collectionUri);
 26:             this.teamService = this.teamProjectCollection.GetService<TfsTeamService>();
 27:             this.identityManagementService = this.teamProjectCollection.GetService<IIdentityManagementService>();
 28:             ICommonStructureService4 cssService = this.teamProjectCollection.GetService<ICommonStructureService4>();
 29:             this.projectInfo = cssService.GetProjectFromName(teamProjectName);
 30:             //Debug.WriteLine(this.identityManagementService.GetType().FullName);
 32:             // 20120612 - WPS: Authenticates the connection to Team Foundation Server if it has not been previously authenticated
 33:             this.teamProjectCollection.EnsureAuthenticated();
 34:         }
 36:         public void Dispose()
 37:         {
 38:             this.teamProjectCollection.Dispose();
 39:         }
 41:         public List<string> ListTeams()
 42:         {
 43:             var teams = this.teamService.QueryTeams(this.projectInfo.Uri);
 44:             return (from t in teams select t.Name).ToList();
 45:         }
 47:         public Guid CreateTeam(string team, string description)
 48:         {
 49:             TeamFoundationTeam t = this.teamService.CreateTeam(this.projectInfo.Uri, team, description, null);
 50:             return t.Identity.TeamFoundationId;
 51:         }
 53:         public bool AddUser(string team, string user, out string message)
 54:         {
 55:             message = string.Empty;
 56:             bool ret = true;
 57:             TeamFoundationTeam t = this.teamService.ReadTeam(this.projectInfo.Uri, team, null);
 58:             TeamFoundationIdentity i = this.identityManagementService.ReadIdentity(IdentitySearchFactor.AccountName, user, MembershipQuery.Direct, ReadIdentityOptions.None);
 60:             if (t == null)
 61:             {
 62:                 message = "Team [" + team + "] not found";
 63:                 ret = false;
 64:             }
 66:             if (i == null)
 67:             {
 68:                 message = "User [" + user + "] not found";
 69:                 ret = false;
 70:             }
 72:             if (ret)
 73:             {
 74:                 this.identityManagementService.AddMemberToApplicationGroup(t.Identity.Descriptor, i.Descriptor);
 75:                 message = "User added ";
 76:             }
 78:             return ret;
 79:         }
 80:     }
 81: }


ALM Ranger Contributor(s) Mattias Sköld
ALM Ranger Reviewers Michael Fourie | Rui Melo | Willy-Peter Schaub
Product Group Reviewer(s) / SME Ben Amodio | Gregg Boer | Hakan Eskici | Mario Rodriguez

Future plans

  • Revise the sample code to include more administrative features
  • Work with the ALM Rangers / ALM MVPs to build a command line tool for the community
  • Add sample code to show the use of LiveIDs, for example when working with TFS Preview

More ideas?


Also please add your candid feedback and comments to this post!

Leave a Comment
  • Please add 8 and 6 and type the answer here:
  • Post
  • My Quick Response Sample - a PowerShell module to manage Team Foundation Server Teams and Users.


    By implementing the exact same functionality as the sample command-line utility with a PowerShell module instead, we get a much richer model for post-processing the results of the commands and much easier ways to provide bulk input to the commands.


  • @J Stangroome, thanks for sharing your PowerShell module which is, as you stated, a powerful alternative. Giulio Vian, who is our Mr. PowerShell, will love your quick response sample :)

  • Wow, we always Impressed with this kind of work, keep up!

  • We have just fixed the Quick Response download package on vsarguidance.codeplex.com/.../96222. To avoid further unplanned over-writes, we have created a quick response dedicated download package.

Page 1 of 1 (4 items)