ObjectSharp
Published in

ObjectSharp

Get all GitHub Repos with PowerShell

During a migration from Azure Repos to GitHub for a large customer, I have written many scripts to help along the way. One of the most useful things is to be able to iterate over all the repos in a GitHub organization to do various things. (ie: Check CODEOWNERS, see who has access, check visibility)

Let me just say the GitHub Rest API is awesome. Easy to use and well documented.

When you call the rest api to get an organizations repos, the default is to return 30 repositories. However you can use the queries per_page and page to get more.

  • per_page — defaults to 30 but can be set to a max of 100.
  • page — Set the page # to get the next page of 30 repos, or whatever per_page is set to.

Since I often wanted all of the repos, I scripted a loop in PowerShell that gets all the repos into one array. I thought I would share, and document for myself at the same time.

$orgName="https://api.github.com/orgs/MyOrg"
$PAT="[Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(":$($env:GH_PAT"))"
$allRepos = @()
$page=0
#keep getting repos until the count comes back zero
do
{
$page += 1
$params = @{'Uri' = ('{0}/repos?page={1}&per_page=100' -f
$orgName, $page )
'Headers' = @{'Authorization' = 'Basic ' + $PAT}
'Method' = 'GET'
'ContentType' = 'application/json'}
$repos = Invoke-RestMethod @params
$allRepos += $repos
$repoCount = $repos.Count
} while($repoCount -gt 0)
#now you have an array of all the repos in your org
foreach ( $repo in $allRepos )
{
#iterate over the Repos and do what you need
$repoName = $repo.name
$repoVisibility = $repo.visibility
$repoArchived = $repo.archived
}

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Dave Lloyd

Dave Lloyd

I have been writing software and teaching/coaching developers for over 35 years. My current passion is Azure DevOps and all the things it can do for a team.