Querying the TeamCity Rest API with Powershell

The following powershell function demonstrates how to get the last successful TeamCity build number using the TeamCity REST API. This is a useful tool for continuous deployment to dev/test environments. I only cared about the build number but with some minor modifications of the XPath parsing you can get a lot of different information.


  • REST API Plugin: Ensure you have the plugin installed. On the build server website go to Administration > Server Administration > Plugins List You should see the entry "REST API."

  • Authentication: You will need to either enable guest access in TeamCity settings or first authenticate via the TeamCity API. Guest access allows a read-only view of our build server website and API.

  • Parameters: You will need to know your project id and TeamCity base url. You can determine the project id by seperate queries but for the simplicity of this example I have hard coded the id.

    Uses guest access to get the lastest successful build for a given project (project id is bt30)

    Guest Access needs to be enabled on TeamCity server or you will need to provide your
    own authentication.

    Example usage:
        Get-Latest-TeamCity-Build "http://teamcityserver/ "bt30" | Write-Output

Function Get-Latest-TeamCity-Build([string]$baseUrl, [string]$projectId) {

    $url = "${baseUrl}guestAuth/app/rest/buildTypes/id:${projectId}/builds?status=SUCCESS"

    $xml = [xml](invoke-RestMethod -Uri $url -Method GET)
    $xpath = "/builds/build[1]"
    $latestBuild = Select-xml -xpath $xpath -xml $xml

    return @{
        "Build" = $latestBuild.Node.GetAttribute("id");
        "Revision" = $latestBuild.Node.GetAttribute("number");
        "Url" = $url

View the latest version of this function on GitHub.