Creating a Github Gist with PowerShell

Recently I posted a PowerShell tool for creating a GitHub repository. In continuing my exploration of the GitHub API I wrote another PowerShell tool to create a GitHub gist. A gist is simple way to store and share snippets or code samples. I use them to share simple PowerShell scripts or other works that aren’t full blown multi-file modules. Now I can create these gists directly from PowerShell and the PowerShell ISE.

As in my repo tool, you will need to know your Github token. My previous article has information about it if you missed it. Once you have that, then you can use this function, which is conveniently stored as a Github gist.

The function will create a gist from content you pass or you can specify a file name and the command will use that content. You will have to specify a name for your gist and you will need your token. I’ve also included an option to create a private gist which I believe requires a paid account.

With this function I can easily create a gist from an existing file.

You can see that gist here. If you use -Passthru you will get a custom object which includes the url.

image

But I also wanted an easy way to take a snippet from the PowerShell ISE and create a gist so I wrote this:

This code includes commands to add short cut menus to the ISE.

image

You can select a block of code in the PowerShell ISE and then click one of these options. You will be prompted in the console pane to provide a filename and description.

What I don’t have now is a good way to update an existing gist or delete them from PowerShell. The GitHub API supports these concepts but I haven’t gotten that far.

In the mean time, I hope you’ll give these commands a try and let me know what you think.

3 thoughts on “Creating a Github Gist with PowerShell

  1. Awesome; I really like this – come to think of it I’m kinda shocked I haven’t happened upon a VS Code extension that builds this ability in (maybe it’s there, but I haven’t seen it yet).

Leave a Reply

Your email address will not be published. Required fields are marked *