How To Use GitHub PowerShell Copilot via PowerShell Module
These instructions explain how to install the PowerShellAI module and use PowerShell Copilot to assist with tasks.
August 1, 2023
A few months ago, Microsoft unveiled its upcoming product called Microsoft 365 Copilot. The tool aims to automate tasks in Microsoft 365 apps through text queries. A user must simply describe what it is they want to create, and Copilot does the rest.
Microsoft also revealed plans to extend Copilot to other products, including Windows, the Edge browser, and Bing. However, Microsoft conspicuously left out PowerShell from the Copilot expansion.
While Microsoft may develop a PowerShell Copilot in the future, there’s no need to wait: An independent, ChatGPT-based Copilot for PowerShell is already available on GitHub. In the spirit of other Microsoft Copilots, this tool lets you describe the task you want PowerShell to perform without requiring knowledge of PowerShell coding.
The PowerShell Copilot is just one component of a broader module called PowerShellAI (you can find it here). In addition to the PowerShell Copilot, this module offers other useful features like an AI-based error helper and the ability to retrieve DALL-E images. It’s worth spending some time examining the various tools included in this module.
How To Install the PowerShellAI Module
Installing the PowerShellAI module is simple. You don’t even have to manually download the module from GitHub.
All you do is open PowerShell and enter this command:
Install-Module PowerShellAI
After entering the command, you might receive a warning message about installing a module from an untrusted repository. When prompted, just press Y to ignore the warning and go forward with the installation.
Note: If you have previously installed the ChatGPT API on your system, you will need to append the -AllowClobber switch to the Install-Module command.
You can refer to Figure 1 for an illustration of the installation process.
PowerShell Copilot 1_0
Figure 1. How to install the PowerShellAI module.
Linking the PowerShellAI Module to Your OpenAI Key
Once you have installed the PowerShellAI module, there’s one more step before you can use its features. You need to provide the module with your OpenAI key, which grants access to ChatGPT programmatically. Without the OpenAI key, none of the module’s tools will work.
To create the key, visit this OpenAI page and click the “Create New Secret Key” button.
Although OpenAI makes ChatGPT free to use on the ChatGPT website, accessing it programmatically through the PowerShell module comes with a cost. As such, you must set up a paid account with OpenAI before you can use the PowerShell Copilot or any other tool within the PowerShellAI module. If you need additional help with creating and configuring your API key, be sure to check out my recent article, “How To Build ChatGPT-Enabled PowerShell Scripts.”
After acquiring the necessary key from OpenAI and setting up a paid account, you must link this key to the PowerShellAI module. To do so, just enter this command:
<a id="powershell copilot" name="powershell copilot"></a>Set-OpenAIKey
When you run the command, PowerShell will prompt you to enter your OpenAI key. Simply type or paste your key, then press Enter. The module is now fully provisioned and ready to use.
How the PowerShell Copilot Works
Now that the setup process is complete, you can begin using the PowerShell Copilot. The Copilot is designed to be unobtrusive and won’t be on the screen until you need it.
To engage Copilot, type the word Copilot in PowerShell, followed by the query enclosed in quotation marks.
As an example, let’s say you want to use PowerShell to find out how many files exist in the current folder, but you’re unsure of the necessary commands. Instead of taking the time to search for the commands, you can simply type the following:
Copilot “show me how many files are in the current folder”
Copilot will then assist you with the PowerShell code to execute the task.
It’s important to note that Copilot has a safety mechanism to prevent it from running code on your system without your permission. Rather than automatically executing a command and displaying the result, Copilot will show you the command it intends to run and then ask for your permission. To allow the command to execute, simply press Y.
PowerShell Copilot 2
Figure 2. This is how PowerShell Copilot works.
For example, in Figure 2, I asked Copilot to show me how to find out the number of files in the current folder. Copilot returned this command:
Get-ChildItem | Measure-Object | Select-Object -ExpandProperty Count
After pressing Y to allow the command to run, Copilot provided a result of 126, indicating that there are 126 files in the current folder.
Keep in mind that my query was merely an example. You can ask Copilot to assist you with a wide range of PowerShell tasks.
About the Author
You May Also Like