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.
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
).
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.