PowerShell Copilot is a community-developed utility for generating PowerShell code and scripts. While it is not officially supported by Microsoft or OpenAI, it can be a helpful tool.
In this video tutorial, Brien Posey explains how to install the PowerShell Copilot module in PowerShell, demonstrates several use cases (e.g., checking disk space), and highlights the tool’s capabilities and limitations.
The following transcript has been lightly edited for length and clarity.
Brien Posey: Hello, greetings, and welcome. I'm Brien Posey. In this video, I want to show you how to install and how to use PowerShell Copilot.
- Install PowerShell Copilot Module
- PowerShell Copilot Basics
- Using PowerShell Copilot for Systems Management
- Using PowerShell Copilot To Create Apps
- Final Thoughts
Now, as you may have heard, Microsoft is creating AI-based Copilots for any number of its products. Having said that, however, PowerShell Copilot is not a Microsoft product. Instead, it's something open source that was developed by the community. It's not something that Microsoft created. So, you can acquire PowerShell Copilot from GitHub.
Now, the good news is that you don't have to go to GitHub and manually download it. You can do the download and installation directly through PowerShell, and it's super easy to do. I'll show you how to do that in just a minute.
Install PowerShell Copilot Module
Before you go ahead and install PowerShell Copilot, there's one thing that you're going to need. What you're going to need is an API key from OpenAI. So, if you go to the OpenAPI website and log in, you'll be taken into your account. From there, you can request an API key. Now, there is a small charge associated with using an API key. But assuming you're just casually using PowerShell Copilot, those charges shouldn't be all that much.
Once you've got your API key, be sure and keep it safe. It's not the sort of thing that you want to disclose to anyone. Just make a note of it. You're going to need that to complete the installation process.
I'm going to switch over to an elevated PowerShell session and install PowerShell Copilot.
The way that we do that is we install a module called PowerShell AI. This module comes directly from OpenAI. So, I'm going to type:
Now, typically, when you go to install this module, you're going to see a message telling you that you're installing from an untrusted repository. If you get this message, just press “Y” for yes or “A” for yes to all.
There is one other message that you might see here. If you've previously installed this module and there are remnants of it on your system, then you'll have to append the -AllowClobber parameter to the Install-Module cmdlet. That will allow the Install-Module cmdlet to overwrite whatever is already on your system.
For right now, I'm just going to go ahead and press Y for yes. So, the module has been installed. But before we can use the module, we have to provide the module with our API key. The way that we do that is by typing:
PowerShell Copilot Basics
Okay, so now that Copilot has been installed, let's try using it. The way that you engage Copilot is simply by typing the word “copilot.” From there, just put whatever you want to ask Copilot in quotation marks.
I'm going to start by saying hello. I'll type:
This doesn't really accomplish anything. This is just a way of verifying that Copilot is working and that it's installed correctly. So, I'll go ahead, and press Enter. And you can see that Copilot is “thinking.” And now we get something that comes back and says:
What I want to show you right here is that typically Copilot is not simply going to provide you with a text-based answer the way that ChatGPT would. Instead, what it's going to give you is PowerShell code that will give you the answer that you're looking for. So, here Copilot isn't just saying, “Hello,” back to you. Instead, it's giving you a PowerShell command: Write-Output “Hello”.
Then you have the option of either running the code [Y], explaining the code [E], copying the code [C], or quitting [Q]. Incidentally, quitting is the default behavior. So, if you just press Enter, it will drop you back out to a command prompt.
Let's try this again. We can explore some of the various options. So, I'll repeat the command, and this time press Y to go ahead and run the code. The code executes and I get the word, “Hello.”
Let's try it again. This time what I'm going to do is I'm going to press E for Explain. What I get is a summary that comes from the various resources that are available online saying Write-Output as a cmdlet in PowerShell that's used to display output in the console. In this example, it will display the string “hello” in the console. So, if you're not super-familiar with PowerShell, this is a great way of learning, because you can have Copilot generate code for you and then Copilot can also explain what that code does and how it works. That's a great way to become familiar with the PowerShell environment if you're not already.
So, I'm going to press Enter to quit.
Using PowerShell Copilot for Systems Management
Let's go ahead and try some other things with Copilot. Let's begin by asking Copilot some questions about our system. Remember, PowerShell started out as a management interface that Microsoft created for Windows. So, let's find out how Copilot works with regard to managing our system.
I'm going to ask Copilot how many files are in the current folder. I'll type:
copilot “Show me how many files are in the current folder”
I’ll press Enter. And once again, what I get is a PowerShell cmdlet. We have Get-ChildItem –that's the PowerShell equivalent to the old dir command that we have from the days of DOS. And then we have Measure-Object. Measure-Object will show us the number of files that are returned by the Get-ChildItem command. So, I'll press Y to run it. And then we can see that this particular folder contains 4,415 files.
What else can we do with Copilot? How about checking how much disk space we have left? Now there are easy PowerShell commands to do this. I wouldn't normally have to use Copilot for that. But let's just see what Copilot does. I'm going to type:
copilot “How much disk space do I have left”
What's interesting is I'm able to put in a natural language query. I don't have to structure this in any specific way, other than making sure that I use the word “copilot” and making sure that I enclose my query in quotation marks.
So, I'll press Enter. And again, I have a command that's returned. In this case, we're using Get-WmiObject, so Copilot is clearly capable of generating more advanced commands. I'll press Y to go ahead and run this. Here, we can see the amount of free space that we have on each one of our disks as well as the total disk size.
Now, if you're wondering why we have to press Y to execute the command instead of just having the command executed automatically, it’s a safety feature. Remember, all these commands are being generated by AI. You never know for sure what the AI engine is going to give you. So, you have a chance to review the command before you execute it. That way, if the AI gives you something really off the wall that has the potential to harm your data or harm your system, you have the chance to opt out of running that command rather than having that command executed automatically.
Let's try some other things. Let's find out if I am logged in as an admin. I haven't tried this ahead of time, so I'm not even sure what's going to happen here. But let's try it.
copilot “Am I logged in as an admin?”
I'll press Enter. And so, we have a couple of commands that are returned. I'm going to press Y to run them, and this time Copilot fails. So, Copilot isn't perfect. There are some problems that Copilot can’t help you with. But that doesn't mean we can't use Copilot for other things.
Using PowerShell Copilot To Create Apps
Let's try a few other things. The next command that I'm going to try is “create a simple Hello World app.” So, I'll type:
copilot “Create a simple hello world app”
I'll press Enter. And what Copilot gives us is a simple Write-Host statement: Write-Host “Hello World!” So, we have a one-line script.
I'll press Yes to run it, and Copilot displays the words, “Hello world!” And you'll notice that it even appended an exclamation point to Hello World, which I didn't ask for. So, there is a bit of creativity here.
What else can we do? Well, what about making a graphical Hello World app? I'll type:
copilot “Create a simple graphical hello world app”
We're given commands for a graphical app. I'm going to press Y to run this. And here we have the words “Hello World” inside of a Windows Form. So, PowerShell Copilot is indeed capable of generating GUI-based apps.
Let's try one more thing that I'm a little bit curious about. Recently, for one of my articles, I created a calculator app that was entirely based on PowerShell. Let's go ahead and take a look at that.
You can see that we have the Posey’s Simple Calculator. This is just a simple calculator app that was written entirely in PowerShell using Forms, and it does work. We can type 123 + 456 = and then we get an answer. That's what my calculator app does.
Let's see if Copilot can generate something similar to this. I wrote this from scratch and didn't use Copilot. So, I'll type:
copilot “generate a script that acts as a calculator”
I'll press Enter. And we have the resulting script. It looks like this time we're using Read-Host to populate two variables: $num1 and $num2. Then we're using an operation: plus, minus, multiply, or divide. And we're combining all of that to produce a result. So, this is a really simple text-based calculator.
Let's go ahead and run it. It says, “Enter the first number.” We'll type 123. “Enter the second number.” I'll type 456. And then “Enter the operation.” I’ll enter “+.” Then we'll get a result. Interestingly, it looks like PowerShell Copilot got this wrong. The result is 123456. So, what really happened was that PowerShell combined two strings. It didn't add those two strings together. So, this isn't really a calculator. Instead, what we're doing is string concatenation.
Let's ask for a graphical calculator and see if PowerShell Copilot can do any better.
I'm going to repeat my command from before and I'll ask for a graphical script:
copilot “generate a graphical script that acts as a calculator”
Remember that Copilot is developed by the community, so it's a helpful utility, but it's not something that's going to be officially supported by Microsoft, OpenAI, or anyone else.
Even so, if you're stuck on creating something in PowerShell, oftentimes Copilot can at least get you a good starting point. It might not always give you exactly what it is that you're looking for, but in most instances, it will give you enough to at least get you started. Again, if it does give you something and you don't fully understand it, you can have Copilot explain that to you by using the Explain option.
So, with that said, I'm Brien Posey. Thanks for watching.Brien Posey is a bestselling technology author, speaker, and 21x Microsoft MVP. In addition to his ongoing work in IT, Posey has trained as a commercial astronaut candidate in preparation to fly on a mission to study polar mesospheric clouds from space.em>