Microsoft's Team Foundation Server (TFS) is a version control, work tracking, build deployment and release management application. It's a product that help software developers and DevOps teams control the entire code creation and deployment process. Although TFS does have a variety of graphical methods to use it, there are times when teams need to automate various aspects of TFS. Luckily, it includes a REST API so we can manage TFS with PowerShell.

TFS provides a rich API which can be used in conjunction with PowerShell to build a handy way to control TFS with the command line and via PowerShell script.

In this article, we're going to cover the basics of what it takes to make that initial connection to TFS with PowerShell. The code you'll be using can then be used to explore more API operations with PowerShell to build more advanced functionality.

Getting Started

To access the TFS REST API first requires authentication. To do this in PowerShell, you can use a PSCredential object. To create a PSCredential object, run Get Credential and provide an account that has access to TFS

PS51> $credential = Get-Credential

Once you have a credential saved as a variable, you'll then use this throughout numerous calls to the PowerShell cmdlet Invoke-RestMethod. Invoke-RestMethod is a PowerShell cmdlet that allows us to craft and send various HTTP requests to REST APIs quickly. Since I'll need to use the Credential parameter for each Invoke-RestMethod call, I'll create a splatted parameter set with Credential inside so I don't have to specify it every time.

$invRestMethParams = @{
    Credential = $credential
}

Now send a test query to TFS just to ensure you don't receive an error. Try to retrieve all of the projects available to you on the server. You can do this by sending the HTTP GET method to the API. But first, you need to know the URI. To retrieve all of the projects, the URL scheme will look like this:

http(s)://<TFSServer>/<TeamCollection>/<TeamProject>/_apis/projects?api-version=2.0

You can see that I'm using v2.0 of the API. My particular URI looks like this:

$uri = 'http://tfs.domain.local:8080/tfs/IT/_apis/projects?api-version=2.0'

Now that you know the URI, you can then add this to the parameters to Invoke-RestMethod.

PS51> $invRestMethParams.Uri = $uri

Next, I'll need to add the other parameters to Invoke-RestMethod.

$invRestMethParams.Method = 'Get'
$invRestMethParams.ContentType = 'application/json'

## My final parameters and the Invoke-RestMethod call will look like this:

$invRestMethParams = @{
    Credential = $credential
    Uri = $uri
    Method = 'Get'
    ContentType = 'application/json'
}
Invoke-RestMethod @invRestMethParams

When I run the above code, you'll get an output that looks like this:

Finding TFS projects with PowerShell

This isn't too helpful. To get at what you're looking for, you need to look at the contents of the value property. You'll immediately see once you look at the value property, each of the projects inside of the collection will show up.

Drilling down on TFS projects

This means that you've authenticated to TFS and have queried the correct URI.

At this point, the world is your oyster. Perhaps you'd like to see all of the builds available to you. Because I've stored all of the parameters inside of the hashtable, I just have to change the Uri parameter.

$invRestMethParams.Uri = 'http://tfs.domain.local:8080/tfs/IT/IS-DevOps/_apis/build/builds?api-version=2.0'
Finding TFS builds with PowerShell

You can see that after you get authenticated and figure out the correct URL scheme, it's just a matter of changing up the URL to do what you want. To learn a lot more about what you can do with the TFS REST API, I encourage you to check out the Visual Studio getting started documentation.

Summary

Using PowerShell, you can begin managing and automating many things with TFS. Managing TFS with PowerShell allows you to integrate various TFS processes into concepts like a CI/CD pipeline among other things.

Join the Jar Tippers on Patreon

It takes a lot of time to write detailed blog posts like this one. In a single-income family, this blog is one way I depend on to keep the lights on. I'd be eternally grateful if you could become a Patreon patron today!

Become a Patron!