Simple PowerShell Script to ask GPT 4 API Questions and Follow-Up Questions

Опубликовано: 09 Июнь 2026
на канале: SysAdmin Tools Com
266
10

Remember to Subscribe!

PowerShell Code and JSON example to ask OpenAI Chat GPT questions and follow up questions.

https://sysadmin-tools.com/blogs/news...

If you struggle to copy the script from the description, you can find it in the blog article linked above.

############################
$apiKey = "PasteYourAPIKeyHere"
$apiUrl = "https://api.openai.com/v1/chat/comple..."

$headers = @{
'Authorization' = "Bearer $apiKey"
'Content-Type' = 'application/json'
}

$conversationHistory = @()

Function GPT-Question ($question) {
$global:conversationHistory += @{
role = 'user'
content = $question
}

$body = @{
model = 'gpt-4'
messages = $conversationHistory
temperature = 1
max_tokens = 256
top_p = 1
frequency_penalty = 0
presence_penalty = 0
} | ConvertTo-Json

$response = Invoke-RestMethod -Uri $apiUrl -Method Post -Headers $headers -Body $body -ContentType 'application/json'

$responseContent = $response.choices[0].message.content
$conversationHistory += @{
role = 'system'
content = $responseContent
}

return $responseContent
}

Example usage:
GPT-Question -question "what is the speed of light"
GPT-Question -question "Can you explain it in more detail?"
############################

https://elevenlabs.io/