Skip to content

Instantly share code, notes, and snippets.

@cmj2002
Last active August 24, 2023 02:39
Show Gist options
  • Save cmj2002/97d0741e3daaab06794b6113332698b2 to your computer and use it in GitHub Desktop.
Save cmj2002/97d0741e3daaab06794b6113332698b2 to your computer and use it in GitHub Desktop.
Call the GitHub Copilot CLI installed in WSL from PowerShell to provide AI-generated command suggestions for PowerShell

Copilot CLI for Powershell

This script enables calling the GitHub Copilot CLI installed in WSL from PowerShell to provide AI-generated command suggestions for PowerShell. It is modified based on @MattJeanes' script.

Bridge script

The following bash script is used to activate nvm and call github-copilot-cli:

#!/bin/bash
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"  # This loads nvm

github-copilot-cli $@

You need to add execution permission to this script (chmod +x copilot-cli-bridge.sh).

PowerShell script

Note: You need to modify the paths in the script to yours, including:

  • $WSLPATH = "/mnt/c/Users/10643/AppData/Local/Temp/$($TMPFILE.Name)", change the dir to your $Env:TMP's WSL path (appears three times)
  • /home/cmj/projects/scripts/copilot-cli-bridge.sh, change to your bridge script's WSL path (appears three times)
function Invoke-CopilotWhatTheShell {
    $TMPFILE = New-TemporaryFile;
    $WSLPATH = "/mnt/c/Users/10643/AppData/Local/Temp/$($TMPFILE.Name)";
    try {
        wsl /home/cmj/projects/scripts/copilot-cli-bridge.sh what-the-shell ('use powershell to ' + $args) --shellout $WSLPATH
        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';

function Invoke-CopilotGitAssist {
    $TMPFILE = New-TemporaryFile;
    $WSLPATH = "/mnt/c/Users/10643/AppData/Local/Temp/$($TMPFILE.Name)";
    try {
        wsl /home/cmj/projects/scripts/copilot-cli-bridge.sh git-assist $args --shellout $WSLPATH
        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;
    $WSLPATH = "/mnt/c/Users/10643/AppData/Local/Temp/$($TMPFILE.Name)";
    try {
        wsl /home/cmj/projects/scripts/copilot-cli-bridge.sh gh-assist $args --shellout $WSLPATH
        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';

Save it as copilot.ps1 and run . copilot.ps1 to "source" these functions and alias.

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