Loading...

Manage GitHub easily with PowerShell

Reply A+ A-

In this post I will show you how to play easily with your GitHub account with PowerShell.
I will show you how to connect, list your repos, create repo, upload files and readme...

The PowerShell module
In order to play with GitHub there is a really cool module available build by the PowerShell team.
This one is called PowerShellForGithub and is available on the PSGallery.
You can install it using the below command:
See here the GitHub repository.
See here how to use it.
Now let's see a bit how to use it.

What to do with the module ?
First of all before to use it, the correct approach is to see available cmdlets in the module.
Fo that use the below command:
This will list all cmdlets as below:
Now if you want to list available cmdlet for a specific part like repository filter as below:
This will list cmdlets for repository.
You can do many thing to manage your GitHub account with the module.
- Manage user profile
- Manage issues
- Manage comments
- Manage repositories
- And more, more, more...

Generate your token
The first step is to generate a token from your GitHub account in order to connect from PowerShell.
For that proceed as below:
1. Go to this link
2. Select what you want to do on your GitHub account
3. In my case I selected evrything just for my tests.
4. Click on Generate a token
5. The token will be generated. Copy it somewhere in order to keep it for new use.

Connect to GitHub with PowerShell
Now we have generated our token, we will connect to our GitHub account with PowerShell.
1. To connect we will use the cmdlet Set-GitHubAuthentication, as below:
2. This will prompt for your token, you can add what you want in the user name

3. Now you are connected
4. You can also use a creds object as below:

List all repositories
To list repository the cmdlet to use is Get-GitHubRepository.
You can list all repositories as below:

List all YOUR repositories
Now we will list all repositories from whih you are owner.
The first step is to get the name of the owner meaning you.
For that we will use the cmdlet Get-GitHubUser, as below
Now let's list all repositories for the specific owner:

Remove a repository
The cmdlet to use to remove a specific repository is Remove-GitHubRepository.
You have to add to add two parameters
- ownername: for the owner
- RepositoryName: for the repository to remove

See below the command to remove a repo test.

Remove all your repositories
Be careful with this, it will remove all of your repo.
We will first list all our repos, then remove them.
For that use the below script:

Remove multiple repositories
Now if you want to remove not all repositories but just some specific we can imagine another way.
For that we will proceed as below:
1. List all repositories name in a txt file, as below:
2. Now open the txt file and delete entries you don't want to remove. The file should only contain repository you want to remove.
3. Now use the below script to remove those repositories:

Create a repository
For that the cmdlet to use it New-GitHubRepository.
Just add the parameter RepositoryName with the name of the repo to create.

Create multiple repositories
Now we will proceed as for the remove multiple repos.
We will use the txt file that contains repos to remove and we hav already removed.

Uploading files on repo
For now there is no specific cmdlet for this.
We will use the cmdlet Invoke-GHRestMethod that allows you to create custom queries.

Now we have to give a look to the GitHub API doc to understand how to use it.
Here this link

Regarding the doc the query should be a below:
PUT /repos/:owner/:repo/contents/:path

This informs us that the method to use us PUT.
The rest of the query can be translated as below:
Your repo path from the owner + contents + file to upload

For instance we want to upload the file Backup_TechNet_Gallery_Contributions.ps1 on the repo test, infos will be as below:
1. Repo path: https://api.github.com/repos/damienvanrobaeysdev/test
2. Then add contents
3. Finally add Backup_TechNet_Gallery_Contributions.ps1

See below the full query path
The documentation also informs us that we have to add a JSON part, as below:
The next step is to encode your file to base 64.
For that use the below code part:
Now we have our file encoded to base 64.
We can now upload the file as below:
See below the full script:
You can also us the same script for ZIP, RAR, EXE, TXT...
psgallery 308293115424152206

Enregistrer un commentaire

Accueil item

Award

Learn KQL in one month

Sponsors

You want to support me ?

Mes articles en français

Books in French


Stats