Skip to content

How to Set Check-in Policies for all Projects in Team Foundation Server using PowerShell?

The Team Foundation Server 2013 provides the administrators with opportunity to add check-in policies to the Source Control Settings.  These check-in policies define that while checking in the code in the Team Foundation Server Version Control (either TFS Version Control or GIT), the user has to perform certain extra actions. These actions vary from adding comments to describe the check-in, linking the check-in to one or more work items defined the in the product backlog or to make sure that your build successfully passes all the unit tests defined in the project.

The check-in policies are defined on the Team Project level. However when you have a Team Project Collection with hundreds of various team projects, you want a way with which you can standardize the check-in policies for all the team projects.

Unfortunately,  Team Foundation Server does not support this functionality out of the box. But luckily, you can use the Team Foundation Server SDK to implement this programmatically. The PowerShell script below shows how to set Check-in Policies for all Team Projects in Team Foundation Server using PowerShell.

The script makes use of the SetCheckinPolicies on the Team Project object. What it does is basically retrieving all the available projects in the Team Project Collection and looping through all available projects to set the check-in policy individually.

You would notice that the script makes use of two policies:

  • Work Item
  • Check for Comments

However, you can extend this or change to select other installed policies on your workstation. You would notice that script makes use of InstalledPoliciyTypes on the work station. It basically makes use of the registered policy assemblies from the registry under the path:

 HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\VisualStudio\12.0\TeamFoundation\SourceControl\Checkin Policies

TFS Policy


[void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.TeamFoundation.Client")
[void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.TeamFoundation.Common")
[void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.TeamFoundation.VersionControl.Client")
[void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.TeamFoundation.VersionControl.Controls")


function AddPolicyOnProject($project){
	$policies = @()
	
	$policies += AddToPolicyEnvelope($workItemPolicy)
	$policies += AddToPolicyEnvelope($checkForComments)
	
	$project.SetCheckinPolicies($policies)
	
	Write-Host "Adding Policies to" $project.Name
}

function AddToPolicyEnvelope($policy){
	$policyType = $installedPolicyTypes | where {$_.Name -eq $policy.Type}
	return New-Object -TypeName Microsoft.TeamFoundation.VersionControl.Client.PolicyEnvelope -ArgumentList @($policy, $policyType)
}

$serverName = "http://manasbhardwaj.net/tfs/Projects"

$tfs = [Microsoft.TeamFoundation.Client.TeamFoundationServerFactory]::GetServer($serverName)
$versionControlType = [Microsoft.TeamFoundation.VersionControl.Client.VersionControlServer]
$versionControlServer = $tfs.GetService($versionControlType)
$projects = $versionControlServer.GetAllTeamProjects($true)

$installedPolicyTypes = [Microsoft.TeamFoundation.VersionControl.Client.Workstation]::Current.InstalledPolicyTypes

$workItemPolicy = New-Object -TypeName Microsoft.TeamFoundation.VersionControl.Controls.WorkItemPolicy
$checkForComments = New-Object -TypeName CheckForCommentsPolicy.CheckForComments

$projects | foreach { AddPolicyOnProject $_ }


Published inUncategorized

Be First to Comment

Leave a Reply

Your email address will not be published. Required fields are marked *