Skip to content

Instantly share code, notes, and snippets.

@MattJeanes
Last active February 5, 2024 09:06
Show Gist options
  • Star 14 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save MattJeanes/839c7adfef085f4b6001927e3eb96fd4 to your computer and use it in GitHub Desktop.
Save MattJeanes/839c7adfef085f4b6001927e3eb96fd4 to your computer and use it in GitHub Desktop.
Alias commands to use GitHub Copilot CLI in PowerShell
# You should insert this script into your PowerShell Profile script so it exists in every session
# Fun fact: This script was mostly generated by ChatGPT by giving it the bash version of the output
# from `github-copilot-cli alias -- "$0"` with a few fixes from me
function Invoke-CopilotWhatTheShell {
$TMPFILE = New-TemporaryFile;
try {
github-copilot-cli what-the-shell $args --shellout $TMPFILE
if ($LASTEXITCODE -eq 0) {
if (Test-Path $TMPFILE) {
$FIXED_CMD = Get-Content -Raw $TMPFILE;
Invoke-Expression $FIXED_CMD;
}
else {
Write-Host "Apologies! Extracting command failed";
}
}
else {
Write-Error "Apologies! Copilot failed to generate a command";
}
}
finally {
Remove-Item $TMPFILE;
}
}
Set-Alias '??' 'Invoke-CopilotWhatTheShell';
Set-Alias 'wts' 'Invoke-CopilotWhatTheShell';
function Invoke-CopilotGitAssist {
$TMPFILE = New-TemporaryFile;
try {
github-copilot-cli git-assist $args --shellout $TMPFILE
if ($LASTEXITCODE -eq 0) {
if (Test-Path $TMPFILE) {
$FIXED_CMD = Get-Content -Raw $TMPFILE;
Invoke-Expression $FIXED_CMD;
}
else {
Write-Host "Apologies! Extracting command failed";
}
}
else {
Write-Error "Apologies! Copilot failed to generate a command";
}
}
finally {
Remove-Item $TMPFILE;
}
}
Set-Alias 'git?' 'Invoke-CopilotGitAssist';
function Invoke-CopilotGitHubAssist {
$TMPFILE = New-TemporaryFile;
try {
github-copilot-cli gh-assist $args --shellout $TMPFILE
if ($LASTEXITCODE -eq 0) {
if (Test-Path $TMPFILE) {
$FIXED_CMD = Get-Content -Raw $TMPFILE;
Invoke-Expression $FIXED_CMD;
}
else {
Write-Host "Apologies! Extracting command failed";
}
}
else {
Write-Error "Apologies! Copilot failed to generate a command";
}
}
finally {
Remove-Item $TMPFILE;
}
}
Set-Alias 'gh?' 'Invoke-CopilotGitHubAssist';
@LucasPlacentino
Copy link

Works great, thanks!

@ViktorKarlstrom
Copy link

Awesome, works like a charm.

@luizhcrocha
Copy link

Thanks

@LuisAndrino
Copy link

Thank you, works fine πŸ‘

@marty0678
Copy link

Fantastic thanks!

@halfprice06
Copy link

halfprice06 commented May 4, 2023

When copilot cli needs to perform more than one action in powershell this gist fails:

PS C:\Users\danielp> ?? create a .txt file and inside the file write a haiku about sunsets

 ──────────────────── Command ────────────────────

New-Item -Path . -Name "haiku.txt" -ItemType "file"
Add-Content -Path "haiku.txt" -Value "The sun is setting"
Add-Content -Path "haiku.txt" -Value "The sky is turning orange"
Add-Content -Path "haiku.txt" -Value "The day is over"

 ────────────────── Explanation ──────────────────

β—‹ New-Item is used to create a new item, e.g. a file.
  β—† -Path . specifies that we want to create the file in the current directory.
  β—† -Name "haiku.txt" specifies the name of the file.
  β—† -ItemType "file" specifies that we want to create a file.
β—‹ Add-Content is used to add content to a file.
  β—† -Path "haiku.txt" specifies the file to add content to.
  β—† -Value "..." specifies the content to add.

πŸ•˜  Hold on, executing commmand...
Invoke-Expression : Cannot convert 'System.Object[]' to the type 'System.String' required by parameter 'Command'.
Specified method is not supported.

@qqii
Copy link

qqii commented May 25, 2023

@halfprice06, @MattJeanes

To support multi line commands, add -Raw to Get-Concent.

@MattJeanes
Copy link
Author

I've updated the script with your suggestion @qqii, thank you!

@SerajMuftah
Copy link

@MattJeanes
Been trying to use Copilot CLI for almost a month now and I just came across this and it worked!
Thank you!

@bruno-srio
Copy link

bruno-srio commented Jul 31, 2023

Thanks Matt, it's working fine πŸ‘

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment