Querying TFS for Checkin Information with Powershell

One of the things that quickly became aparent when I joined my present company six months ago was the laid back approach to commenting code checkins/commits. I’m working on changing that and have had moderate success leading with by example, but the time is coming when I’ll have to start naming and shaming! To that end, I started investigating how to write a powershell script that will calculate the percentage of checkins that had a comment.

Firstly, you’ll need to install the TFS Power Tools.

Next, you can call “Add-PSSnapin Microsoft.TeamFoundation.PowerShell” to get access to the powershell cmdlets.

Finally, you can call “Get-TfsItemHistory” to get information about checkins.

Here’s my script in full

# Add the snapin if its not already added
trap {
Add-PSSnapin Microsoft.TeamFoundation.PowerShell

# Move to my local workspace folder
push-location C:MyWorkspaceFolder

# Get all the checkin info for the last 7 days
$history = Get-TfsItemHistory . -Recurse -Stopafter 1000 | Where {$_.CreationDate -gt (get-date).AddDays(-7)}

# Group by owner, figure out how many checkins they did, how many were
# commented and what the coverage was as a percentage. Finally, sort by
# that coverage percentage

$history |
group Owner |
select Name,
@{Name="Commented";Expression={($_.Group | where { $_.Comment -ne $null }).Count}} |
select Name,
@{Name="Coverage";Expression={[math]::round(($_.Commented / $_.Checkins) * 100.0, 2)}} |
sort "Coverage" -desc

# Move back to the starting directory