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.
New-GitHubGist -Name "BoxPrompt.ps1" -Description "a fancy PowerShell prompt function" -Path S:\boxprompt.ps1
You can see that gist here. If you use -Passthru you will get a custom object which includes the url.
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.
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.