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.
ManageEngine ADManager Plus - Download Free Trial
Exclusive offer on ADManager Plus for US and UK regions. Claim now!
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.
https://gist.github.com/jdhitsolutions/06cb62bf3eb4f0a1f7d82ed39b1e56ca
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:
https://gist.github.com/jdhitsolutions/345a6b8e4c47440df5fb1dbcb987cb3e
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.
I wrote a module to manipulate gists a while ago. It includes an update function: https://gist.github.com/XPlantefeve/5e85af5998abf08f2d86c9aef968ef9f
Feel free to use it as an inspiration as to what you think it should look like. Feel free to critic the code as well, it was my “let’s learn to code a module/let’s learn to use an API” moment.
Thank you!
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).