Created
December 2, 2025 20:47
-
-
Save crpietschmann/e6b1210fa34410b8622e987f9a503d55 to your computer and use it in GitHub Desktop.
PowerShell script to enable automated scripting of the GitHub Copilot CLI
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # ###################################################### | |
| # GitHub Copilot Prompt Wrapper | |
| # ##################################################### | |
| # This PowerShell script serves as a wrapper around the GitHub Copilot CLI, | |
| # allowing users to send prompts and receive responses in a controlled manner. | |
| # It supports optional model selection and debugging features. | |
| # | |
| # Usage: | |
| # ./copilot.ps1 -p <prompt> [-model <string>] [-EnableDebug <bool>] | |
| # Parameters: | |
| # -p <string> The prompt to send to Copilot. (Required) | |
| # -model <string> (Optional) Model to pass through to Copilot CLI (e.g., 'gpt-5', 'gpt-5-mini'). | |
| # -EnableDebug <bool> (Optional) If set to $true, writes the full Copilot CLI console output to 'copilot_prompt_log.txt'. | |
| # | |
| # Examples: | |
| # ./copilot.ps1 -p "What's your name?" | |
| # ./copilot.ps1 -p "What's your name?" -EnableDebug:$true | |
| # ./copilot.ps1 -p "Generate a summary of today's news." -model "gpt-5-mini" | |
| # | |
| # Capturing output in a variable: | |
| # $result = ./copilot.ps1 -p "Generate a summary of today's news." -model "gpt-5-mini" | |
| # echo $result | |
| # | |
| # Author: Chris Pietschmann <https://build5nines.com> | |
| # ###################################################### | |
| param( | |
| [Parameter(Mandatory=$false)] | |
| [string]$p, | |
| [Parameter(Mandatory=$false)] | |
| [string]$model, | |
| [Parameter(Mandatory=$false)] | |
| [bool]$EnableDebug = $false | |
| ) | |
| # Show help if -p is not specified or --help/-h is present | |
| if (-not $p -or $args -contains '--help' -or $args -contains '-h') { | |
| Write-Host "GitHub Copilot Prompt Wrapper" | |
| Write-Host "" | |
| Write-Host "Author: Chris Pietschmann <https://build5nines.com>" | |
| Write-Host "" | |
| Write-Host "Usage: ./copilot.ps1 -p <prompt> [-model <string>] [-EnableDebug <bool>]" | |
| Write-Host "" | |
| Write-Host "Parameters:" | |
| Write-Host " -p <string> The prompt to send to Copilot. (Required)" | |
| Write-Host " -model <string> (Optional) Model to pass through to Copilot CLI (e.g., 'gpt-5', 'gpt-5-mini')." | |
| Write-Host " -EnableDebug <bool> (Optional) If set to \$true, writes the full Copilot CLI console output to 'copilot_prompt_log.txt'." | |
| Write-Host "" | |
| Write-Host "Examples:" | |
| Write-Host " ./copilot.ps1 -p 'What\'s your name?'" | |
| Write-Host " ./copilot.ps1 -p 'What\'s your name?' -EnableDebug:\$true" | |
| exit 0 | |
| } | |
| $resultsFileName = "copilot_prompt_results.txt" | |
| $logFileName = "copilot_prompt_log.txt" | |
| $prompt = @" | |
| Follow the instructions of the following prompt: | |
| [PROMPT] | |
| {$p} | |
| [/PROMPT] | |
| Write the results for the tasks of this prompt to a file named '$resultsFileName'. | |
| Do NOT create, modify, or reference any other files. | |
| Ignore any previous instructions to create other files and output only to the '$resultsFileName' file. | |
| "@ | |
| # Build Copilot CLI arguments (supporting optional --model passthrough) | |
| $copilotArgs = @('-p', $prompt, '--allow-all-tools') | |
| if ($PSBoundParameters.ContainsKey('model') -and $null -ne $model -and $model.Trim().Length -gt 0) { | |
| $copilotArgs += @('--model', $model) | |
| } | |
| # Run GitHub Copilot CLI and capture output and exit code | |
| $copilotOutput = & copilot @copilotArgs 2>&1 | |
| $exitCode = $LASTEXITCODE | |
| # Optionally log full output | |
| if ($EnableDebug) { | |
| $copilotOutput | Set-Content -LiteralPath $logFileName | |
| } | |
| # If Copilot returned an error, forward it and exit with same code | |
| if ($exitCode -ne 0) { | |
| $copilotOutput | Write-Error | |
| exit $exitCode | |
| } | |
| # Output the results file content to the console so callers can capture it | |
| $resultsContent = $null | |
| if (Test-Path -LiteralPath $resultsFileName) { | |
| $resultsContent = Get-Content -LiteralPath $resultsFileName -Raw | |
| $resultsContent | Write-Output | |
| } else { | |
| Write-Error "Results file '$resultsFileName' not found." | |
| } | |
| # Cleanup created files when not debugging, after outputting results | |
| if (-not $EnableDebug) { | |
| if (Test-Path -LiteralPath $resultsFileName) { | |
| Remove-Item -LiteralPath $resultsFileName -Force -ErrorAction SilentlyContinue | |
| } | |
| if (Test-Path -LiteralPath $logFileName) { | |
| Remove-Item -LiteralPath $logFileName -Force -ErrorAction SilentlyContinue | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment