Scott Hanselman

GitHub Copilot for CLI for PowerShell

April 25, 2023 Comment on this post [6] Posted in AI | PowerShell
Sponsored By

GitHub Next has this cool project that is basically Copilot for the CLI (command line interface). You can sign up for their waitlist at the Copilot for CLI site.

Copilot for CLI provides three shell commands: ??, git? and gh?

This is cool and all, but I use PowerShell. Turns out these ?? commands are just router commands to a larger EXE called github-copilot-cli. So if you go "?? something" you're really going "github-copilot-cli what-the-shell something."

So this means I should be able to to do the same/similar aliases for my PowerShell prompt AND change the injected prompt (look at me I'm a prompt engineer) to add 'use powershell to.'

Now it's not perfect, but hopefully it will make the point to the Copilot CLI team that PowerShell needs love also.

Here are my aliases. Feel free to suggest if these suck. Note the addition of "user powershell to" for the ?? one. I may make a ?? and a p? where one does bash and one does PowerShell. I could also have it use wsl.exe and shell out to bash. Lots of possibilities.

function ?? { 
$TmpFile = New-TemporaryFile
github-copilot-cli what-the-shell ('use powershell to ' + $args) --shellout $TmpFile
if ([System.IO.File]::Exists($TmpFile)) {
$TmpFileContents = Get-Content $TmpFile
if ($TmpFileContents -ne $nill) {
Invoke-Expression $TmpFileContents
Remove-Item $TmpFile
}
}
}

function git? {
$TmpFile = New-TemporaryFile
github-copilot-cli git-assist $args --shellout $TmpFile
if ([System.IO.File]::Exists($TmpFile)) {
$TmpFileContents = Get-Content $TmpFile
if ($TmpFileContents -ne $nill) {
Invoke-Expression $TmpFileContents
Remove-Item $TmpFile
}
}
}
function gh? {
$TmpFile = New-TemporaryFile
github-copilot-cli gh-assist $args --shellout $TmpFile
if ([System.IO.File]::Exists($TmpFile)) {
$TmpFileContents = Get-Content $TmpFile
if ($TmpFileContents -ne $nill) {
Invoke-Expression $TmpFileContents
Remove-Item $TmpFile
}
}
}

It also then offers to run the command. Very smooth.

image

Hope you like it. Lots of fun stuff happening in this space.

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.

facebook twitter subscribe
About   Newsletter
Hosting By
Hosted in an Azure App Service
April 26, 2023 0:13
Cool stuff, just signed up for the waitlist. I'm already a CoPilot paid subscriber, would be interesting to try it out in CLI
April 26, 2023 14:34
Copilot is already available from the PowershellAI module. And it is real fun. At least it asks for a confirmation before executing the command. Is this a different one?
April 26, 2023 17:21
When copilot suggests more than one line / two commands, the Invoke-Expression throws that it cannot convert an System.Object[] to a 'System.String' required by paramater 'Command'.
Any idea how to resolve that?
April 26, 2023 18:11
While you wait, ai-shell by @builder-io is a fantastic command line helper that generates shell commands from natural language: “find all .git folders created in the last week and write them to newprojects.txt”

https://github.com/BuilderIO/ai-shell
April 26, 2023 23:33
Instead of invoking the expression, you should use the PSReadline API to inject the returned code into the input buffer. I had planned on writing something that used GPT4 API to do this but since you're already on it and I have a disgusting pool to clear up... 😁
April 28, 2023 18:52
While Copilot and derivatives can be handy, it is basically in the same sense, as stolen car also is handy. Microsoft can go away with this only because of its size and resources, but it does not change the fact the Copilot is based on stolen data (which it is even amusing a bit, they could target companies to steal software from, but they chose only open-source projects -- likelihood of being sued by freelancer is much less than being sued by some company). "MS loves open-source" has new meaning :-).

Comments are closed.

Disclaimer: The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.