Using the Azure CLI to query Azure DevOps

April 30, 2024

In this post, I’m going to cover writing a Powershell script that interrogates Azure DevOps to return projects, repositories, and PRs by user.

I’ve previously covered installing the Azure CLI, and some basic uses of that.

As stated in those articles, the first step is to install the DevOps extension:

az extension add --name azure-devops

We can now play about with some of the devops commands; for example, this will list all the projects in an organisation:

az devops project list --org $organisationURL --query "*[].name" -o tsv

You can replace the $organisationURL with the actual URL for your organisation; it will probably look like this:

The query allows you to pull a specific set of data from the returned JSON (you can see this by simply removing the query and see what is returned).

Here’s a simple Powershell script that gets a list of team projects, returns a list of repos for each, and all PRs for each repo:

# Replace these variables with your organisation URL and username
$organisationURL = ""
$username = "[email protected]"

Write-Output "Running"

# Get list of projects
$projects = az devops project list --org $organisationURL --query "*[].name" -o tsv | ForEach-Object { $_.Trim() }

# Iterate over each project
foreach ($project in $projects) {
    Write-Output "Project: $project"

    # Get list of repositories in the project
    $repositories = az repos list --project $project --org $organisationURL --query "[].name" -o tsv | ForEach-Object { $_.Trim() }    

    # Iterate over each repository in the project
    foreach ($repository in $repositories) {
        Write-Output "Repository: $repository"
        az repos pr list --repository $repository --project $project --org $organisationURL --status all --query "[].title" -o tsv | ForEach-Object { $_.Trim() }    

In the references below, you can see a more detailed explanation of the possible arguments. You can, for exmaple, change the az repos pr list to return only PRs by a given person:

#az repos pr list --repository $repository --project $project --org $organizationURL --status all --query "[].title" -o tsv | ForEach-Object { $_.Trim() }    


Azure CLI Reference


Profile picture

A blog about one man's journey through code… and some pictures of the Peak District

© Paul Michaels 2024