Last active
March 25, 2026 13:56
-
-
Save dfinke/2dfdeec22fa8a1fd2a4b01705b8e99bd to your computer and use it in GitHub Desktop.
A PowerShell function mimicking Claude Code's /loop, repeatedly executing AI prompts at natural-language time intervals.
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
| #Requires -Modules PSAISuite | |
| function loop { | |
| <# | |
| .SYNOPSIS | |
| Executes a prompt via Invoke-ChatCompletion multiple times with a specified time interval. | |
| .DESCRIPTION | |
| The loop function takes a natural language string containing a prompt and an optional time interval. | |
| It runs the specified prompt using Invoke-ChatCompletion up to 5 times. | |
| If an interval is provided (e.g., "5m", "every 20 minutes"), it sleeps for that duration between runs. | |
| If no interval is provided, the default sleep time is 1 minute. | |
| Valid interval units are s (seconds), m (minutes), h (hours), and d (days). | |
| .PARAMETER InputArgs | |
| The prompt and optional interval, passed as remaining arguments. | |
| .EXAMPLE | |
| loop 5m check the deploy | |
| Runs "check the deploy" 5 times, waiting 5 minutes between each run. | |
| .EXAMPLE | |
| loop check the deploy every 20m | |
| Runs "check the deploy" 5 times, waiting 20 minutes between each run. | |
| .EXAMPLE | |
| loop run tests every 5 minutes | |
| Runs "run tests" 5 times, waiting 5 minutes between each run. | |
| .EXAMPLE | |
| loop check the deploy | |
| Runs "check the deploy" 5 times, with the default interval of 1 minute between each run. | |
| #> | |
| param( | |
| [Parameter(ValueFromRemainingArguments)] | |
| [string[]]$InputArgs | |
| ) | |
| $fullText = $InputArgs -join ' ' | |
| # Default interval: 1 minute | |
| $sleepSeconds = 60 | |
| $prompt = $fullText | |
| # Extract time interval like "5m", "every 20m", "every 5 minutes" | |
| if ($fullText -match '(?i)(?:every\s+)?(\d+)\s*(s|m|h|d|sec|min|hour|day)[a-z]*\b') { | |
| $value = [int]$Matches[1] | |
| $unit = $Matches[2].Substring(0, 1).ToLower() | |
| $sleepSeconds = switch ($unit) { | |
| 's' { $value } | |
| 'm' { $value * 60 } | |
| 'h' { $value * 3600 } | |
| 'd' { $value * 86400 } | |
| } | |
| # Remove the specified interval from the actual prompt string | |
| $prompt = ($fullText -replace $Matches[0], '').Trim() | |
| } | |
| if ([string]::IsNullOrWhiteSpace($prompt)) { | |
| Write-Warning "Prompt is empty." | |
| return | |
| } | |
| $tools = $( | |
| 'Get-Content' | |
| 'Get-ChildItem' | |
| 'Get-Location' | |
| ) | |
| # Run max 5 times | |
| for ($i = 1; $i -le 5; $i++) { | |
| Write-Host "Loop $i of 5: Running prompt '$prompt'" -ForegroundColor Cyan | |
| $messages = @( | |
| @{role = 'system'; content = 'you are in a powershell console. use the tools to get the job done. all ops done in current directory. list files if needed, use wildcards' } | |
| @{role = 'user'; content = $prompt } | |
| ) | |
| Invoke-ChatCompletion -Messages $messages -Tools $tools -Model openai:gpt-4.1 | |
| if ($i -lt 5) { | |
| Write-Host "Sleeping for $sleepSeconds seconds..." -ForegroundColor DarkGray | |
| Start-Sleep -Seconds $sleepSeconds | |
| } | |
| } | |
| } |
Author
dfinke
commented
Mar 22, 2026
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment