Category Archives: Powershell

Say Hello to ChatGPT with Powershell

ChatGPT, Bard, and other AI models are fun. A good way to dig into their capability and limitations are to access them via their API or applications programming interface. From what I’ve seen, many examples of this are in Python. So far, I hadn’t seen much for Powershell…so I thought it would be interesting to say hello to ChatGPT using Powershell.

Like any API, you need to establish an authorization code which identifies you to the API and which allows billing. The authcode essentially serves as a name and password for your call to ChatGPT. First you have to create an account:

Sign Up for OpenAI API

To sign up for the OpenAI API, visit OpenAI’s website and fill out the form. Once your request is approved, you’ll receive an invitation to join the API.

Get an API Key

After you’ve been accepted into the API, you’ll need to generate an API key. To do this, navigate to the API Dashboard and click on the “Generate API Key” button. Your key will be a string of alphanumeric numbers about 48 characters long.

Choose your model

OpenAI has several different models going back from ChatGPT4. Each model is a little different. Some are supposed to be more suitable for things like chatbots, others are better for searching, and of course the graphic model Dall-E generates (argueably horrible) pictures. For purposes of our sample here…. we’ll use the “text-Davinci-03” model which is an earlier version of the ChatGPT3 series.

Powershell Code to Connect:

The Powershell Code uses the PowerShell Invoke-RestMethod cmdlet, and its use is surprisingly similar to other API calls for other services. Here is the full code:

$headers = @{
‘Content-Type’ = ‘application/json’
‘Authorization’ = ‘Bearer yourAPICodeGoesHere12334455’

$body = @{
prompt = ‘Hello ChatGPT!’
temperature = 0.7
model = ‘text-davinci-003’

$bodyjson= ConvertTo-Json -inputobject $body

$response = Invoke-RestMethod -Uri ‘’ `
-Method ‘POST’ `
-Headers $headers `
-Body $bodyjson


The prompt parameter specifies the text prompt to send to the model. The temperature parameter controls the “creativity” of the response, with higher values generating more novel responses. The model parameter specifies which model to use – in this case, text-davinci-003.

It works! Run the Powershell script, and you’ll get the following:

‘Hi there! How can I help you today?

API Documentation

You can find more information about the text-davinci-003 model in the OpenAI API documentation. The documentation provides details on the available parameters, example responses, and more.

Payment and Charges

It’s worth noting that to use the OpenAI API, you’ll need to provide a payment method and will be charged an initial $5.00. More information on payment and charges can be found on the API pricing page.

JSON for PowerShell Part 1

JSON, otherwise known as “Javascript object notation” is a fundamental way of representing key/value pairs. In database terms this translates as a field name (the key) and the field contents (the value).

Let’s show a single contact record in JSON, with two phone numbers, work and home.

"Address1":"123 Anywhere Lane",
 "Address2":"Apartment 404",
 {"type":"home","number":"920 234-3424" },
 {"type":"work","number":"920 535-2312"  }

A couple of characteristics:

The complete JSON structure is enclosed in its own curly brackets
If there is more than one record, then the whole structure is contained with square brackets, and the individual records are separated by a comma. This applies to nested records too, as in the case of the two phone numbers for the individual.

Below is the notation for two contact records. In this case, the whole structure is enclosed in square brackets. The individual contact records are enclosed in curly brackets and the two records are separated by a comma.

 "Address1":"123 Anywhere Lane",
 "Address2":"Apartment 404",
"PhoneNumbers":[ {"type":
"home","number":"920 234 3424"},
"type":"work","number":"920 535-2312"}]
"Address1":"123 Anywhere Lane",
"Address2":"Apartment 404",
"PhoneNumbers":[ {"typee":
"home","number":"920 234-3424"},
{"type":"work","number":"920 535-2312"}]

When writing JSON, its helpful to have an automatic checker for syntax. One such is JSONLint at In Visual Studio Code you can use the Shift-Alt-F within the editor to nicely format JSON code.

The rules of JSON are summarized at JSONLint, but basically include:

  • Keys are enclosed in double quotes.
  • All data elements that are not a boolean or integers are enclosed in double-quotes.
  • Individual entities (i.e. records) are separated by a comma
  • Each key/value pair is separated by a comma
  • Each entity is enclosed by curly brackets.

Since I’m editing JSON in Visual Studio Code, there is more how VSC handles JSON in the Microsoft docs at JSON editing in Visual Studio Code.

Powershell has a two commands that deal directly with JSON.

PS M:\PSFolder> get-command *json*     

CommandType     Name                    Version    Source
-----------     ----                    -------    ------
Cmdlet          ConvertFrom-Json    Microsoft.PowerShell.Utility
Cmdlet          ConvertTo-Json    Microsoft.PowerShell.Utility

These convert Powershell objects to and from JSON notation. More on that in Part 2.