GitHub Copilot for CLI in PowerShell

GitHub Copilot for CLI in PowerShell

·

2 min read

I recently got access to the copilot-cli beta from GitHub. Basically it's like GitHub Copilot, but for the command line. You tell it what to do in natural language and it'll give you the command/commands to run. It also offers an explanation for the commands and you can additionally fine-tune your query. If you're satisfied, you can run the command.

Only one problem: support is limited to bash only at the moment and I use PowerShell! I do use WSL but most of my time in the command line is spent in PowerShell. Naturally, I went to the GitHub Next discord server and asked if anyone had found a workaround. Fortunately, for me Murf#2728 had an alias script working. I modified it a bit and came up with my own version with cleaned-up code and proper arguments.

My script to get aliases working:

function Invoke-GithubCopilotCommand {
    [CmdletBinding(DefaultParameterSetName = "Default")]
    param (
        [Parameter(ParameterSetName = "Help")]
        [Switch]$Help,

        [Parameter(ParameterSetName = "Git", Mandatory = $true)]
        [Alias("g")]
        [Switch]$Git,

        [Parameter(ParameterSetName = "GitHub", Mandatory = $true)]
        [Alias("gh")]
        [Switch]$GitHub,

        [Parameter(ValueFromRemainingArguments = $true)]
        [string[]]$RemainingArgs
    )

    $tempFile = "c:\temp\copilot.txt"

    function CheckResponse {
        param (
            [bool]$Success
        )

        if ($Success) {
            $fileContentsArray = Get-Content $tempFile
            $fileContents = [string]::Join("`n", $fileContentsArray)
            Write-Host $fileContents
            if (-not [string]::IsNullOrEmpty($fileContents)) {
                Invoke-Expression $fileContents
            }
        }
        else {
            Write-Host "User cancelled the command."
        }
        Set-Content $tempFile ""
    }

    if (-not (Test-Path "c:\temp")) {
        New-Item -ItemType Directory -Path "c:\temp" | Out-Null
    }

    if ($Help) {
        $codeToRun = "github-copilot-cli help"
        Invoke-Expression $codeToRun
    }
    elseif ($Git) {
        Write-Host "github-copilot-cli git-assist --shellout $tempFile $RemainingArgs"
        github-copilot-cli git-assist --shellout $tempFile $RemainingArgs
        CheckResponse $?
    }
    elseif ($GitHub) {
        Write-Host "github-copilot-cli gh-assist --shellout $tempFile $RemainingArgs"
        github-copilot-cli gh-assist --shellout $tempFile $RemainingArgs
        CheckResponse $?
    }
    else {
        Write-Host "github-copilot-cli what-the-shell --shellout $tempFile powershell $RemainingArgs"
        github-copilot-cli what-the-shell --shellout $tempFile powershell $RemainingArgs
        CheckResponse $?
    }
}

Set-Alias /cp Invoke-GithubCopilotCommand
Set-Alias copilot Invoke-GithubCopilotCommand

You can add this file to your PowerShell profile .ps1 which you can find by running $PROFILE

You can also check out the PowerShell Module uploaded by Temm#9188 on the GitHub Next Discord for a potentially easier setup.