Use ChatGPT OpenAI with PowerShell

The estimated reading time 3 minutes

If you follow the latest digital news you may probably heard about ChatGPT and other OpenAI tools. Tools like ChatGPT can be integrated in our daily work with PowerShell. See this short manual with the most important steps.

First step is to create an API-Key within the ChatGPTP Portal. So you need to sign in or register if not already done so (hope so).

After succesfully login you need to open the following link to get directly to the API-Key generator.
https://platform.openai.com/account/api-keys

NOTE: the key is displayed only ONE TIME. Please use an passwordmanager to store it safely.

Meanwhile there is a nice powershell module created by Doug Finke see the link to the project site. Also the powershell module is available in the PowerShell Gallery see this link.

Installation of this PowerShell Module is quite easy and can be done within PowerShell Console. In my case I’m installing this module as systemwide installation. It also can be installed in user context.

Install-Module -Name PowerShellAI -Scope AllUsers -Verbose

For connecting to the OpenAI Network you need to input the API-Key. So there are two different methods with CMDLET or System variable. I recommend the CMDLET I’ll explain why.

Set-OpenAIKey

Alternatively I mentioned the system variable, which can be done like this.

$env:OpenAIKey = "OPENAI-APIKEY...."

NOTE: If you use the system variable you need to type in or paste the key in clear text, and it is stored unencrypted in the history of your powershell. If you may use Visual Studio Code it may also be stored in a special TXT file where VSCode protocolls all commands entered. Finding this TXT file can be done in console.
If you use other powershell consoles you may can use this cmdlet to clear history.

(Get-PSReadlineOption).HistorySavePath

You can delete this TXT file so VSCode “forgets” this sensitive information.

Asking ChatGPT

We can finally start asking the AI some stupid things.

Webinterface looks kind of sorted, but can also be used to get information.

Get-GPT3Completion "Show me all multifactorauthentications available in Microsoft azure ad as json" 
Get-GPT3Completion "Show me all multifactorauthentications available in Microsoft azure ad as csv file " 

It’s also possible to combine the output and export it to csv , see this example.

$azuremfacsv = Get-GPT3Completion "Show me all multifactorauthentications available in Microsoft azure ad as csv file with Name and Description delimiter semicolon " 
 $azuremfacsv
 $azuremfacsv | Out-File -FilePath C:\temp\azureadmfa.csv -Encoding utf8 

As you can see there is a ChatGPT question output which is converted to an CSV File and can be used in another operation.

AI is not going to replace our jobs. But someone who knows how to use it will

If you like the article pleas klick on “Helpful” or leave me a comment.

Print Friendly, PDF & Email
Was this article helpful?
YesNo
5 1 vote
Article Rating
Subscribe
Notify of
guest
1 Kommentar
Newest
Oldest Most Voted
Inline Feedbacks
View all comments
Paul Fijma
1 year ago

Nice! thanks for sharing!