Getting Started with Cutting Edge PowerShell AI

Published:6 March 2023 - 6 min. read

Nicholas Xuan Nguyen Image

Nicholas Xuan Nguyen

Read more tutorials by Nicholas Xuan Nguyen!

Azure Cloud Labs: these FREE, on‑demand Azure Cloud Labs will get you into a real‑world environment and account, walking you through step‑by‑step how to best protect, secure, and recover Azure data.

Artificial Intelligence (AI) is rising and rapidly transforming how people live and work. With the advent of technologies like machine learning and natural language processing, the possibilities for automation and intelligent decision-making are virtually limitless. Take a cutting-edge PowerShell AI, for example.

With the addition of AI capabilities, PowerShell becomes even more potent. And in this tutorial, you learn the basics of PowerShell AI. Eventually, you can develop intelligent scripts to make complex decisions and learn from their interactions with the environment.

Read on and stay at the forefront of this rapidly-evolving field!

Prerequisites

This tutorial comprises hands-on demonstrations. To follow along, be sure you have the following in place:

  • A system with PowerShell 5.1 or above installed – This tutorial uses Windows 10 with PowerShell 7.2.
  • An OpenAI account with credits to use its services – The API requires credits. The free tier now only offers $5 for 3 months. Once that is expired, or used, you need to add a payment method. Once that payment method is added, it may take up to 10-15 min. for your API queries to start working.

Installing a PowerShell AI (PowerShellAI Module)

The PowerShellAI module adds AI capabilities to PowerShell. This module lets you access pre-built machine-learning models and natural language processing tools. These tools can help develop intelligent scripts and automate complex decision-making tasks.

With this module, incorporate cutting-edge AI features into your script without needing to understand the complex technology behind it. This way, you can focus on what you do best while using the latest and most excellent tools.

Open PowerShell and run the below commands, which do not provide output, but install (Install-Module) and import (Import-Module) the PowerShellAI module.

# Installs the PowerShellAI module
Install-Module -Name PowerShellAI
# Imports the PowerShellAI module to the current session
Import-Module -Name PowerShellAI

Now, run the below Get-Command to verify that the PowerShellAI module has been correctly installed.

This command searches for all the available cmdlets within the PowerShellAI module and displays the properties selected (Select-Object) by Name.

Get-Command -Module PowerShellAI | Select-Object Name
Verifying that the PowerShellAI module has been correctly installed
Verifying that the PowerShellAI module has been correctly installed

Creating an OpenAI API Key

Made by an AI research organization (OpenAI), the OpenAI API provides access to powerful language models that understands and generates human-like text. With this API, developers can automate natural language processing tasks.

Before you can interact with the OpenAI API, you must create an OpenAI API key, which allows you to use the PowerShellAI cmdlet. With this cmdlet, you can make HTTP requests to the OpenAI API endpoints and retrieve the response data in several formats like JSON, XML, and plain text.

To create a new OpenAI API key, follow the steps below:

1. Open your favorite web browser, and navigate to the official OpenAI Manage API Keys page.

Logging into the OpenAI account
Logging into the OpenAI account

2. Next, click Create new secret key to generate a new API key.

Generating a new API key
Generating a new API key

3. Copy, and store the generated API key in a safe place. You will need this API key later for authentication when making requests to the OpenAI API.

Copying the generated API key
Copying the generated API key

Storing your API key in a secure location, such as a password manager or encrypted file, is 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 require additional authentication steps.

4. Now, run the below command, which does not produce output but sets your API key as an environment variable. Replace the Your_API_Key placeholder with the API key you copied in step three.

The API key is sensitive information that should not be hard-coded in your scripts. Storing it as an environment variable is a secure way to ensure it remains protected while allowing you to use it in your scripts.

$env:OpenAIKey = "Your_API_Key"

5. Lastly, run the following echo command to verify if the OpenAI API key has been set correctly as an environment variable.

echo $env:OpenAIKey
Verifying if the OpenAI API key has been set correctly
Verifying if the OpenAI API key has been set correctly

Generating Texts with OpenAI API and PowerShellAI

Once you have set up the OpenAI API key as an environment variable, you can use it regardless of your current working directory. The most common use of the OpenAI API is to generate text based on a given prompt. How?

The PowerShellAI module provides a cmdlet called Get-GPT3Completion that you can use to generate text with the OpenAI API. This cmdlet sends a prompt to the OpenAI API, which then generates a continuation of the prompt based on its understanding of language and context.

1. Run the below command to send the prompt Prints Hello in Italy and French to the OpenAI API and await a response.

Get-GPT3Completion 'Prints Hello in Italian and French.'

Below, you can see the response received is formatted and displayed in the console, including the translations of Hello in Italian and French.

Generating texts with OpenAI API and PowerShellAI
Generating texts with OpenAI API and PowerShellAI

While getting automatically generated text seems cool, fact-checking and reviewing them is still recommended. Doing so ensures accuracy and appropriateness for your use case, especially when dealing with complex or specialized domains.

2. Next, run the following command to generate a JSON string containing the planets list in the solar system.

Get-GPT3Completion 'List of all planets in solar system in JSON format.'
Generating a JSON string that contains the list of solar planets

3. Now, run the below commands to send a prompt to the OpenAI API, store the response to the $Response variable, and print it.

# Sends a prompt to the OpenAI API and stores the response to a variable
$Response = Get-GPT3Completion 'The world would be a better place if'
# Calls/Prints the value of the $Response variable
$Response

In the output below, the response contains a sentence describing how greater empathy and compassion between people could lead to world peace. But note that the response generated by the OpenAI API is not pre-written or pre-defined.

The OpenAI API is powered by deep learning algorithms, generating unique and contextually-relevant real-time responses. With this behavior, the output can vary each time you run the same command, depending on the selected model and other factors.

Sending a prompt and printing the response stored in a variable
Sending a prompt and printing the response stored in a variable

The dynamic nature of the OpenAI API opens up a wide range of possibilities for generating unique and engaging content. You can use the API to create chatbots, generate product descriptions, and write articles or stories.

Generating Codes with Copilot

Besides generating texts, OpenAI offers Copilot, a service designed to improve developer productivity by automating repetitive or boilerplate coding tasks. In turn, developers get to focus on more high-level aspects of software development.

What is so good about this service? With a simple prompt, using the copilot cmdlet, you can generate code for various programming languages, such as Python and JavaScript. And with PowerShellAI, you can access Copilot right from your command line.

To learn more about generating codes with Copilot, follow these steps:

1. Run the below copilot command to request a code snippet in PowerShell that adds two numbers together.

copilot ' Write a PowerShell snippet that adds two numbers'

Below is the generated code for adding two numbers in PowerShell, which may vary depending on the context you provide. After reviewing the code, you also get the option to either run the code or not.

If the code is intended for PowerShell, input Y and press Enter. Otherwise, input N, press Enter, copy the generated code, and paste it to the appropriate platform.

Note that you still have to edit each generated code, like removing the line numbers in front of each line. As incredible as Copilot may seem, it is not magic but your AI pair programmer.

Generating codes with OpenAI API and PowerShellAI
Generating codes with OpenAI API and PowerShellAI

2. Next, run the command below to generate a function that takes two numbers passed in as parameters and returns the sum of those two numbers.

copilot ' Write a PowerShell function that adds two numbers'

Unlike the first generated code, press Enter to choose not to run the code this time.

As you can see, the OpenAI API can also generate complex and lengthy codes.

Generating a function that takes two numbers to add
Generating a function that takes two numbers to add

3. Run the code below that you copied in step two, which does not provide output but loads the Add-TwoNumbers function into memory.

Function Add-TwoNumbers {
	 Param (
		 [int]$Number1, 
		 [int]$Number2
	 )
	 $Number1 + $Number2
}

4. Finally, call the generated code/function (Add-TwoNumbers) with either of the following, passing in two numbers to add (10 and 20).

# Call the Add-TwoNumbers function with parameters
Add-TwoNumbers -Number1 10 -Number2 20
# Or just the parameter values
Add-TwoNumbers 10 20

The output will be 30, as shown below, confirming that the generated works correctly.

Testing the generated PowerShell function
Testing the generated PowerShell function

Conclusion

AI is changing the way people create content and code. And in this tutorial, you have learned that with a cutting-edge PowerShell AI, you can generate unique and engaging content — all with minimal effort.

With PowerShellAI and OpenAI API working side-by-side, you get the Copilot, which provides a convenient way to generate code snippets in various programming languages quickly.

This tutorial only shows the tip of the iceberg. OpenAI and PowerShellAI have a lot more to offer. Why not fine-tune the API to get more accurate results or create complex code snippets?

Hate ads? Want to support the writer? Get many of our tutorials packaged as an ATA Guidebook.

Explore ATA Guidebooks

Looks like you're offline!