How to use thePowerShell AI Module

Computer Uptime MPA Tools

Unleash the Power of AI in PowerShell Scripting

Today, we’re diving into the realm of AI-powered scripting by exploring the PowerShell AI module—an innovative fusion of PowerShell’s strength with the intelligence of OpenAI’s technology. This blog post is intended to be your comprehensive guide, walking you through the process of setting up an OpenAI account, obtaining API keys, and integrating the PowerShell AI module into your workflow.

In many of our previous posts, we have talked about using PowerShell scripts to perform various tasks. Whether it be for deploying Intune Remediation scripts or SCCM Configuration Baselines, or even setting up Windows kiosk mode with the MDM Bridge, the task of developing PowerShell scripts can get quite cumbersome. However, using OpenAI could help alleviate the complexity. From simplifying automation tasks to enhancing efficiency, the integration of AI capabilities within PowerShell is a game-changer.

What is the PowerShell AI module?

The PowerShell AI module is an open source and community developed PowerShell module that interacts directly with OpenAI’s GPT AI with just a few simple commands. Whether you’re a seasoned PowerShell enthusiast or a curious beginner, the PowerShell AI module will help guide you through your script development. For more information, see the GitHub page GitHub – PowerShell AI module for OpenAI GPT-3 and DALL-E.

Creating an OpenAI account

If you already have an account, then select the Log in option. However, if you don’t already have an OpenAI account then first, we will start off by creating one. In you web browser, navigate to OpenAI Developer Platform. Click the Sign Up button on the top right-hand side of the page.

Enter you email address and then click Continue. On the next page, enter your new password and then click Continue.

A screenshot of a computer Description automatically generated

Creating an OpenAI API Key

An OpenAI API key will be required to connect the PowerShell AI module to your OpenAI account. Once you have logged in, you can create a new API key by selecting the API keys option on the left-hand side navigation menu. Alternatively, you could navigate to API keys – OpenAI API.

Click on the option to Create new secret key.

Set a name for the API key and then click Create secret key.

Once the API key has been generated, it will be displayed so that you can copy it for use in your code.

Important: Store your API key in a secure location, such as a password manager or encrypted file, as a good practice. Without the key, you cannot authenticate with the service. Generating a new API key is possible, but this process can be time-consuming and will require additional authentication steps. For more information, see Best Practices for API Key Safety | OpenAI Help Center.

 

Installing the PowerShell AI Module

Now we will install the PowerShell AI module, which will require administrative rights and internet access (to access the PS Gallery). From the Windows Start menu, search for Windows PowerShell, and then select the option to Run as administrator.

Next, we will need to change the execution policy to allow us to install and import the PowerShell AI module. For this example, we will set the execution policy to Bypass with the following command:

Set-ExecutionPolicy -ExecutionPolicy Bypass

 

A screen shot of a computer Description automatically generated

Enter “Y” to complete the change to the execution policy.

Next, we will install the PowerShell AI module with the following command:

Install-Module -Name PowerShellAI

 

A computer screen shot of a black screen Description automatically generated

Enter “Y” to agree to installing the module from the PS Gallery.

A screenshot of a computer Description automatically generated

After which, you will see the progress of the installation of the module.

A screenshot of a computer Description automatically generated

To verify that the module has been installed successfully and to view all the available commands in the module, we can execute the following command:

Get-Command -Module PowerShellAI

 

Updating the module help library

After installing any new PowerShell modules, we would recommend updating the help library so that the help content for the new modules will become available. Updating the PowerShell help can be done with the below command:

Update-Help

 

A screenshot of a computer Description automatically generated

Getting started with the PowerShell AI module

We are almost ready to get started with the PowerShell AI module but before we can use the module, we will first need to complete the following steps:

  1. Import the PowerShell AI module
  2. Set the OpenAI key
  3. Set the Chat API Provider

Importing the PowerShell AI module

When you install a new PowerShell module on a system, you will need to import it so that the module loads into active memory, and so that you can access that module in your PowerShell session. The PowerShell AI module can be imported with the below command:

Import-Module PowerShellAI

 

A screen shot of a computer Description automatically generated

Setting the OpenAI Key

Next, we will need to set the OpenAI API key to connect the PowerShell AI module to your OpenAI account. For this we will need to use the Set-OpenAIKey cmdlet. If we query the cmdlet help with the below command, we can see that the Set-OpenAIKey cmdlet requires the OpenAI key in a secure string. Secure strings are an easy built-in way to manage sensitive information in PowerShell and prevent the string from being viewed in plain text.

Get-Help Set-OpenAIKey -Full

 

A screenshot of a computer Description automatically generated

There are several ways that the OpenAI API key can be converted to a secure string:

  • Use the Get-Secret command from the Microsoft.PowerShell.SecretManagement module. This is the recommended option as shown in the provided example from the help command. However, this option requires setting up a Secret Store vault. For more information, see Get started with the SecretStore module – PowerShell | Microsoft Learn.
  • Use the Read-Host command with the AsSecureString parameter. This is the best option for entering sensitive information directly into the PowerShell window.
  • Use the ConvertTo-SecureString command. This is not the recommended option because it requires that the key be entered into the PowerShell window in plain text.

For this example, we will use the Read-Host option with the below command, and we will assign the API key to the variable $OpenAIKey.

$OpenAIKey = Read-Host -Prompt “Enter Open API key” -AsSecureString

 

A screen shot of a computer Description automatically generated

Now, we can set the Open AI key using the following command:

Set-OpenAIKey -Key $OpenAIKey

 

A screen shot of a computer Description automatically generated

Setting the Chat API Provider

Lastly, we will need to set the default Chat API Provider. According to the PowerShell AI Module GitHub page, there are two Chat API Provider options:

  • AzureOpenAI
  • OpenAI

For this example, we will set OpenAI as the default Chat API Provider with the below command:

Set-ChatAPIProvider OpenAI

 

A screen shot of a computer Description automatically generated

Note: One of the chat API providers must be set before executing any of the below commands.

Testing the PowerShell AI module

The PowerShell AI Module GitHub page provides a few interesting examples to test the AI module.

The examples below demonstrate how you can use the Get-GPT3Completion cmdlet to convert data into JSON or XML format. There are also other examples provided for using the AI function to convert at the pipeline to xml, PowerShell, or a markdown table. We recommend having a look at all the examples provided on the GitHub page.

Get-GPT3Completion “List of all planets in solar system in JSON format.”

 

A screen shot of a computer Description automatically generated

Get-GPT3Completion “list of planets only names as xml”

 

A screen shot of a computer Description automatically generated

A screenshot of a computer Description automatically generated

Using the Copilot function

The examples below demonstrate how you can use the copilot function to prompt OpenAI for the PowerShell code necessary to complete a task.

copilot “using PowerShell regex, just code. split user from domain of email address with match: demo.user@google.com”

 

*Above Copilot example provided by Clem Messerli

A screen shot of a computer Description automatically generated

After the copilot function has provided the suggested PowerShell code to use, you have the additional option to enter “E” for an explanation.

You also have the option to enter “Y” to execute the suggested code to test the results.

In this example, we can use copilot to demonstrate how to use splatting to declare properties.

copilot ‘splat properties using Get-ItemProperty as an example’

 

A screen shot of a computer Description automatically generated

In this example, we can use copilot to demonstrate how to retrieve all installed applications from the Windows registry.

copilot ‘get each application installed from registry listing displayname, displayversion, publisher and installdate’

 

A screen shot of a computer Description automatically generated

Important: Just a quick note at this point to discuss the difference between using PowerShell versus PowerShell ISE, which is my preferred tool for PowerShell development and testing. In our testing, we have found that some of the PowerShell AI functions/cmdlets do not work as expected in PowerShell ISE; for example, we saw the below error using the copilot function in PowerShell ISE.

*These errors relate to the non-support of interactive console features in PowerShell ISE, which is an environment intended only for unattended execution. Meaning, if you need a full, interactive console experience, as you do with the PowerShell AI module, then you should use the standard PowerShell window.

Using the AI Error Helper

Speaking of console errors, this error scenario with PowerShell ISE provides a good opportunity to demonstrate the usage of the Invoke-AIErrorHelper function. This function will query OpenAI for an explanation for the last exception error in the console window.

Invoke-AIErrorHelper

 

Using the Open AI Edit function

Another useful feature of the PowerShell AI module is the Get-OpenAIEdit function, which returns an edited version of what you have provided at the prompt. In this example, you can see that OpenAI can generate the comment-based help for a function.

$Output = Get-OpenAIEdit -InputText @’

function greet {

param($n)

 

“Hello $n”

}

‘@ -Instruction ‘add comment-based help detailed description’

 

A screen shot of a computer Description automatically generated

A computer screen shot of a black screen Description automatically generated

That’s just a few examples of how OpenAI can be leveraged to help you with your PowerShell development. As the technology evolves, the possibilities will become endless!

Executing your script

Remember, once you have developed your PowerShell script, you can easily deploy it with MPA Tools. Simply copy your script to the remote computer using the “Copy Files to Destination Computer” action from the File category in the Action menu. Then, under the Scripts category, execute it with the “Run PowerShell Script Files” action.

Get-ItemProperty HKLM:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\* | Select-Object DisplayName, DisplayVersion, Publisher, InstallDate

 

A screenshot of a computer Description automatically generated

Or you can enter your script content in the “Run PowerShell Scripts” action. Click Run Now and then your script will be executed on the remote computer.

Once the script has been executed on the remote client, we can see the results by clicking the Script Output.

Note: On Windows 10/11 clients, the remote execution of PowerShell scripts requires the Windows Remote Management (HTTP-In) inbound firewall rule for the relevant network profile to be enabled on the client.

Subscribe To Our Newsletter

Subscribe To Our Newsletter

Join our mailing list to receive the latest news and updates from our team.

You have Successfully Subscribed!

Pin It on Pinterest

Share This