In this post, we will go over how to download Azure DevOps repositories using a Managed Identity and REST API. This is a follow-up to the post on how to authenticate to Azure DevOps using a Managed Identity.

List all repositories

Connect-AzAccount -Identity
$token = (Get-AzAccessToken -ResourceUrl "499b84ac-1321-427f-aa17-267ca6975798").Token
$org = "MyOrg"
$project = "SecretSauce"
$uri = "$org/$project/_apis/git/repositories?api-version=7.0" 
$header = @{
    'Authorization' = "Bearer $token"
    'Content-Type' = 'application/json'
$response = Invoke-RestMethod -Uri $uri -Headers $header -Method GET

Now, the response contains the repositories in the project SecretSauce in the organization MyOrg. The response is a JSON object that contains a list of repositories and most importantly, their IDs.

Download a specific repository

Let’s assume we want to download the repository named MyRepo. We can do this by using the following code:

$repoId = ($response.value | Where-Object { $ -eq "MyRepo" }).id
$uri = "$org/$project/_apis/git/repositories/$repoId/items?path=/&"+'$format=zip&download=true'
$repo_response = Invoke-RestMethod -Uri $uri -Headers $header -Method GET -OutFile ""

This will download the repository named MyRepo as a zip file to the current directory.