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
Supported Platforms
- 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
Installation
You can install PSOpenAI from PowerShell Gallery.
Install-Module -Name PSOpenAI
Functions
The full list of functions
Common
- ConvertFrom-Token
- ConvertTo-Token
- Get-CosineSimilarity
- Get-OpenAIContext
- Set-OpenAIContext
- Clear-OpenAIContext
OpenAI
Chat
Guide: How to use Chat
- Enter-ChatGPT
- Request-ChatCompletion
- Get-ChatCompletion
- Set-ChatCompletion
- Remove-ChatCompletion
- New-ChatCompletionFunction
Responses
Guide: Migrate ChatCompletion to Response
Conversations
- New-Conversation
- Get-Conversation
- Set-Conversation
- Remove-Conversation
- Add-ConversationItem
- Get-ConversationItem
- Remove-ConversationItem
Assistants
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
Realtime
Guide: How to use Realtime API
- Connect-RealtimeSession
- Connect-RealtimeTranscriptionSession
- Disconnect-RealtimeSession
- Set-RealtimeSessionConfiguration
- 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.
Images
Audio
Videos
Guide: How to use Video generation
Files
Batch
Guide: How to use Batch
Containers
- New-Container
- Get-Container
- Remove-Container
- Add-ContainerFile
- Get-ContainerFile
- Remove-ContainerFile
- Get-ContainerFileContent
Others
Azure OpenAI Service
Usage
See Docs and Guides for more detailed and complex scenario descriptions.
Responses
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 ...
Chat Completions
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]
Audio Speech (Text-to-Speech)
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
Audio transcription (Speech-to-Text)
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'
Image generation
Creating images from scratch based on a text prompt.
$global:OPENAI_API_KEY = '<Put your API key here.>' Request-ImageGeneration -Model 'gpt-image-1.5' -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.
Image edit
Request-ImageEdit -Model 'gpt-image-1.5' -Prompt 'A bird on the desert' -Image 'C:\sand_with_fether.png' -OutFile 'C:\bird_on_desert.png' -Size 1024x1024
The edited image like this.
| Original | Generated |
|---|---|
![]() |
![]() |
Video generation
Generate a video from a text prompt.
$VideoJob = New-Video -Model 'sora-2' -Prompt "A cat playing piano" -Size 1280x720 $VideoJob | Get-VideoContent -OutFile "C:\output\cat_piano.mp4" -WaitForCompletion
Moderation
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
List available models
Get a list of available models.
$Models = Get-OpenAIModels
Realtime
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.output_text.delta' { $eventItem.delta | Write-Host -NoNewLine -ForegroundColor Blue } } } # Connect to the Realtime session Connect-RealtimeSession -Model 'gpt-realtime' Set-RealtimeSessionConfiguration -OutputModalities '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
Advanced
Multiple conversations keeping context.
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
Streaming responses
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
Vision (Image input)
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?'
Web Search
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
Azure OpenAI Service
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
OpenAI Compatible Servers
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
About API key
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.
Method 1: Set an environment variable named OPENAI_API_KEY. (RECOMMENDED)
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?"
Method 2: Set a global variable named OPENAI_API_KEY.
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?"
Method 3: Supply as named parameter.
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.>'
Changelog
Plans and TODOs.
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.
License & Libraries
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)




