Extracting GitHub Repository Details via REST with PowerShell
This week I decided to do some GitHub housekeeping at my workplace.
We currently have about 120+ repositories spread across three separate GitHub organisation accounts.
This is a pain for several reasons
- Locating source code for any particular project
- Managing users across multiple organisations
- Configuring external integrations - e.g. Azure Devops
The first thing I need to do is create a consolidated list of all the repositories across all the organisations so we can decide what to move, archive, delete etc.
This post is specifically about that.
Fortunately for us, Microsoft has already done a lot of the work for us in the form of the
PowerShellForGitHub PowerShell Module
The process we’re going to follow is
- Create a GitHub personal access token
- Install the PowerShellForGitHub PowerShell Module
- Use our new PowerShell Module to set our GitHub Auth details
- Query GitHub to see the repositories and the properties available
- Refine and Export the data to CSV
Creating a GitHub personal access token
Assuming you’re already logged into your GitHub account in your browser - navigate to the New personal access token page
Provide a name for the access token in the
Note box, and then select the
reposcope is probably overkill for this use case - but I didn’t try to restrict it further.
Once you’ve clicked save, your fresh new token will be displayed - save it somewhere safe, you need it later, and you won’t be able to see it again.
Install the PowerShellForGitHub PowerShell Module
Open a PowerShell session, paste and run the following script.
It will prompt you with an Untrusted Repository warning - use the
A (All) option to continue
Set your GitHub Auth credential
This one has a slight oddity to it, when you run this script, it will prompt you for your
username - you can enter anything, it’s just ignored.
However, when prompted for the password - paste in the personal access token you created in step 1
View all the repositories and the available properties
Now, to list your repositories, run the following.
On the first run, it might download some additional nuget packages before executing.
The result will list out all your repositories and show the available properties.
Refine and Export the data to CSV
Alright, once you’ve decided on the properties you want to export, update the script below and run it.
In my case, I wanted the
updated_at property values, but then also a SubString from the
full_name property, that I’m aliasing to
This is done with a calculated property
You will find the
Repositories.csv file in the same path as your PowerShell session - in my case that was
You can copy the debug output stream from your script to the clipboard automatically using
For example, using the script from earlier, if you append
5>&1 | clip - this will take the output from the PowerShell debug stream, redirect it to the success stream, and pipe it to your clipboard.
After running it, just head on over to your favourite text editor and do a paste.