PowerShell module for OpenAI and Azure OpenAI Service.
You can use OpenAI functions such as ChatGPT, Speech-to-Text, Text-to-Image from PowerShell.
This is a community-based project and is not an official offering of OpenAI.
-
About OpenAI API
https://platform.openai.com/docs -
About Azure OpenAI Service
https://learn.microsoft.com/en-us/azure/ai-services/openai/overview
- Windows PowerShell 5.1
- PowerShell 7 or higher
- Windows, macOS or Linux
You need to sign-up OpenAI account and generates API key for authentication.
https://platform.openai.com/api-keys
You can install PSOpenAI from PowerShell Gallery.
Install-Module -Name PSOpenAI
The full list of functions
- ConvertFrom-Token
- ConvertTo-Token
- Get-CosineSimilarity
- Get-OpenAIContext
- Set-OpenAIContext
- Clear-OpenAIContext
- Enter-ChatGPT
- Request-ChatCompletion
- Get-ChatCompletion
- Set-ChatCompletion
- Remove-ChatCompletion
- New-ChatCompletionFunction
Guide: Migrate ChatCompletion to Response
Guide: How to use Assistants
Guide: How to use File search with Assistants and Vector Store
- Get-Assistant
- New-Assistant
- Set-Assistant
- Remove-Assistant
- Get-Thread
- New-Thread
- Set-Thread
- Remove-Thread
- Get-ThreadMessage
- Add-ThreadMessage
- Remove-ThreadMessage
- Get-ThreadRun
- Start-ThreadRun
- Stop-ThreadRun
- Wait-ThreadRun
- Receive-ThreadRun
- Get-ThreadRunStep
- Get-VectorStore
- New-VectorStore
- Set-VectorStore
- Remove-VectorStore
- Add-VectorStoreFile
- Get-VectorStoreFile
- Remove-VectorStoreFile
- Start-VectorStoreFileBatch
- Get-VectorStoreFileBatch
- Stop-VectorStoreFileBatch
- Wait-VectorStoreFileBatch
- Get-VectorStoreFileInBatch
Guide: How to use Realtime API
- Connect-RealtimeSession
- Connect-RealtimeTranscriptionSession
- Disconnect-RealtimeSession
- Set-RealtimeSessionCofiguration
- Set-RealtimeTranscriptionSessionConfiguration
- Send-RealtimeSessionEvent
- Add-RealtimeSessionItem
- Remove-RealtimeSessionItem
- Request-RealtimeSessionResponse
- Stop-RealtimeSessionResponse
- Start-RealtimeSessionAudioInput *
- Stop-RealtimeSessionAudioInput *
- Start-RealtimeSessionAudioOutput *
- Stop-RealtimeSessionAudioOutput *
[*] Works on Windows with PowerShell 7.4+ only.
See Docs and Guides for more detailed and complex scenario descriptions.
The primary method for interacting with OpenAI models. You can generate text from the model with the code below.
$env:OPENAI_API_KEY = '<Put your API key here.>'
$Response = Request-Response -Model 'gpt-4o' -Message 'Explain quantum physics in simple terms.'
Write-Output $Response.output_text
This code outputs answer from model like this.
Quantum physics is a branch of science that deals with the behavior of ...
The previous standard for generating text is the Chat Completions API. You can use that API to generate text from the model with the code below.
Chat Completions API is compatible with other AI services besides OpenAI, such as GitHub Models and Google Gemini. You can also use self-hosted local AI models with LM Studio or Ollama. That is explained in the Advanced section.
$env:OPENAI_API_KEY = '<Put your API key here.>'
$Completion = Request-ChatCompletion -Model 'gpt-4o' -Message 'Give me a recipe for chocolate cake.'
Write-Output $Completion.Answer[0]
Generates audio from the input text.
$env:OPENAI_API_KEY = '<Put your API key here.>'
Request-AudioSpeech -Model 'gpt-4o-mini-tts' -Text 'Hello, My name is shimmer.' -OutFile 'C:\Output\text2speech.mp3' -Voice shimmer
Transcribes audio into the input language.
$global:OPENAI_API_KEY = '<Put your API key here.>'
Request-AudioTranscription -Model 'gpt-4o-transcribe' -File 'C:\SampleData\audio.mp3'
Creating images from scratch based on a text prompt.
$global:OPENAI_API_KEY = '<Put your API key here.>'
Request-ImageGeneration -Model 'gpt-image-1' -Prompt 'A cute baby lion' -Size 1024x1024 -OutFile 'C:\output\babylion.png'
This sample code saves image to C:\output\babylion.png
.
The saved image like this.
Request-ImageEdit -Image 'C:\sunflower_masked.png' -Prompt 'sunflower' -OutFile 'C:\sunflower_restored.png' -Size 256x256
Masked image is restored to full images by AI models.
Source | Generated |
---|---|
![]() |
![]() |
Test whether text complies with OpenAI's content policies.
The moderation endpoint is free to use when monitoring the inputs and outputs of OpenAI APIs.
PS C:\> $Result = Request-Moderation -Text "I want to kill them."
PS C:\> $Result.results.categories
# True means it violates that category.
sexual : False
hate : False
violence : True
self-harm : False
sexual/minors : False
hate/threatening : False
violence/graphic : False
Get a list of available models.
$Models = Get-OpenAIModels
The Realtime API enables you to communicate with AI models live, in real time experiences.
Here's a basic text-based example. For more detailed usage, please refer to the guide. Guide: How to use Realtime API
$env:OPENAI_API_KEY = '<Put your API key here>'
# Subscribe to events
Register-EngineEvent -SourceIdentifier 'PSOpenAI.Realtime.ReceiveMessage' -Action {
$eventItem = $Event.SourceArgs[0]
switch ($eventItem.type) {
'response.text.delta' {
$eventItem.delta | Write-Host -NoNewLine -ForegroundColor Blue
}
}
}
# Connect to the Realtime session
Connect-RealtimeSession -Model 'gpt-4o-realtime-preview'
Set-RealtimeSessionConfiguration -Modalities 'text' -Instructions 'You are a science tutor.'
# Send messages to the AI model
Add-RealtimeSessionItem -Message 'Why does the sun rise in the east and set in the west?' -TriggerResponse
# Disconnect
Disconnect-RealtimeSession
Request-Response
and Request-ChatCompletion
accepts past dialogs from pipeline. Additional questions can be asked while maintaining context.
PS C:\> $FirstQA = Request-ChatCompletion -Model 'gpt-4.1-nano' -Message 'What is the population of the United States?'
PS C:\> Write-Output $FirstQA.Answer
As of October 2023, the estimated population of the United States is approximately 336 million people.
PS C:\> $SecondQA = $FirstQA | Request-ChatCompletion -Message 'Translate the previous answer into French.'
PS C:\> Write-Output $SecondQA.Answer
En octobre 2023, la population estimée des États-Unis est d'environ 336 millions de personnes.
PS C:\> $ThirdQA = $SecondQA | Request-ChatCompletion -Message 'Just tell me the number.'
PS C:\> Write-Output $ThirdQA.Answer
336 millions
By default, results are output all at once after all responses are complete, so it may take some time before results are available. To get responses sooner, you can use the -Stream
option for Request-ChatCompletion
and Request-Response
Request-ChatCompletion 'Describe ChatGPT in 100 charactors.' -Stream | Write-Host -NoNewline
You can input images to the model and get answers.
# Local file
$Response = Request-Response -Model 'o4-mini' -Images 'C:\SampleData\donut.png' -Message 'How many donuts are there?'
# Remote URL
$Response = Request-Response -Model 'o4-mini' -Images 'https://upload.wikimedia.org/wikipedia/commons/5/5f/Cerro_El_%C3%81vila_desde_El_Bosque_-_Caracas.jpg' -Message 'Where is this?'
Allow models to search the web for the latest information before generating a response.
$Response = Request-Response -Model 'gpt-4.1' -Message 'What was a tech news in Merch 2025?' -UseWebSearch
If you want to use Azure OpenAI Service instead of OpenAI. You should create Azure OpenAI resource to your Azure tenant, and get API key and endpoint url. See guides for more details.
$global:OPENAI_API_KEY = '<Put your api key here>'
$global:OPENAI_API_BASE = 'https://<resource-name>.openai.azure.com/'
Request-ChatCompletion `
-Model 'gpt-4o' `
-Message 'Hello Azure OpenAI Service.' `
-ApiType Azure
If you want to use OpenAI compatible services such as GitHub Models, Google Gemini, self-hosted servers like LM Studio or Ollama.
# This is an example for GitHub Models.
$global:OPENAI_API_KEY = '<Put your GITHUB_TOKEN>'
$global:OPENAI_API_BASE = 'https://models.github.ai/inference'
Request-ChatCompletion `
-Model 'microsoft/Phi-4-reasoning' `
-Message 'What is the capital of France?' `
-ApiType OpenAI
Almost all functions require an API key for authentication.
You need to sign-up OpenAI account and generates API key from here.
https://platform.openai.com/api-keys
There are three ways to give an API key to functions.
Set the API key to the environment variable named OPENAI_API_KEY
.
This method is best suited when running on a trusted host or CI/CD pipeline.
PS C:> $env:OPENAI_API_KEY = '<Put your API key here.>'
PS C:> Request-ChatCompletion -Message "Who are you?"
Set the API key to the $global:OPENAI_API_KEY
variable. The variable is implicitly used whenever a function is called within the session.
PS C:> $global:OPENAI_API_KEY = '<Put your API key here.>'
PS C:> Request-ChatCompletion -Message "Who are you?"
Specify the API key explicitly in the ApiKey
parameter. It must be specified each time the function is called.
This is best used when the function is called only once or with few calls, such as when executing manually from the console.
PS C:> Request-ChatCompletion -Message "Who are you?" -ApiKey '<Put your API key here.>'
If you have a feature request or bug report, please tell us in Issue.
- More docs, samples.
- Performance improvements.
- Add a support for fine-tuning.
This module uses these OSS libraries.
- Newtonsoft.Json by jamesnk (MIT License)
- NJsonSchema by rsuter (MIT License)
- Microsoft.DeepDev.TokenizerLib by microsoft (MIT License)
- NAudio by Mark Heath (MIT License)