r/PowerShell • • Mar 08 '23

PowerGPT: Run commands using natural language using GPT model

https://github.com/ouromoros/PowerGPT

Hi, I wrote a PowerShell module that offers a quick way to utilize power of GPT model to assist performing tasks in PowerShell. Basically it works like this:

$ PowerGPT "list all files in current folder with created date"
Will execute script:
-----
Get-ChildItem | Select-Object Name, CreationTime
-----

The functions and code are a little unpolished now but I already find it really handy to use. I would love to hear your opinions on it. Welcome to have a try :)

115 Upvotes

26 comments sorted by

31

u/lanerdofchristian Mar 08 '23

Props for not just running arbitrary code from a tool that only understands what code should look like.

Personally, I'm afraid this will still cause problems for less mindful users who put too much stock in what AI can currently do.

5

u/_ouromoros Mar 08 '23

You're definitely right. I deliberately designed the flow so that user needs to confirm before actually executing the script, but perhaps we can do more engineering to filter out "dangerous commands" and at least put a warning.

4

u/MeanFold5714 Mar 08 '23

Just tack on -WhatIf to everything and halt anything that doesn't have a -WhatIf switch.

2

u/CitySeekerTron Mar 08 '23

If you integrate it into a voice model, you could even implement overrides and MFA for certain elevations.

Think about it: "Computer: Delete all files in the Windows System32 directory, override Dukat-5-1-1-6-Green"

2

u/compwiz21 Mar 09 '23

I’ve already built. I call it lockdown. Generates a random password which is sent to our ticketing system. Then it goes and logs the pc out. The entire office πŸ˜ƒ. You can even integrate it with voice biometrics.

3

u/[deleted] Mar 08 '23

Going to give this a try .ill be back

3

u/[deleted] Mar 08 '23 edited Mar 08 '23

This is super helpful when it comes to trying to find out how to do something in powershell very fast

i know i just cant type anything but was testing ,

is this normal for you to see this error after something like this

PS:> powergpt "copy all files from Documents to desktop in a new folder then open youtube.com"

The description is a little vague, do you mean:

[0] Copy all files from Documents to desktop in a new folder.

[1] Create a new folder on desktop and copy all files from Documents to the new folder.

[2] Copy all files from Documents to a new folder on desktop and open youtube.com.

Choose one description that matches your task, or [n]o:

When i type 2 i get

Invoke-RestMethod: { "error": { "message": "You didn't provide an API key. You need to provide your API key in an

Authorization header using Bearer auth (i.e. Authorization: Bearer YOUR_KEY), or as the password field (with blank

username) if you're accessing the API from your browser and are prompted for a username and password. You can obtain

an API key from https://platform.openai.com/account/api-keys.", "type": "invalid_request_error",

"param": null, "code": null } }

Either way this is super cool!

6

u/_ouromoros Mar 09 '23

Just fixed it, a huge bug caught on sight, shame on me. You can update to `1.0.2` and hopefully it will work. Thanks for trying!

0

u/[deleted] Mar 08 '23

[deleted]

5

u/_ouromoros Mar 09 '23

omg, I'm going to fix it immediately.

13

u/Billi0n_Air Mar 08 '23

Writing shell scripts can be difficult, sure...but boring?

C'mon Jack (in my joe biden voice)

4

u/YT-Deliveries Mar 09 '23

There's also a plugin for VS Code called "Rubberduck" that I've really liked: https://marketplace.visualstudio.com/items?itemName=Rubberduck.rubberduck-vscode&ref=producthunt

1

u/gate122 Mar 09 '23

I just downloaded VS Code 2 days ago. Still a learning curve to set up like Powershell ISE, which I am use to. Do you know any VS Code extension for Powershell that can imitate the cmdlet menu list in Powershell ISE?

1

u/YT-Deliveries Mar 09 '23

Sorry, I don’t. I really never used that feature. I’m old so I tend to use IDEs as if they were just better looking versions of EMACS

1

u/gate122 Mar 09 '23

Thanks. I found a workaround. I install the latest PSReadline and turn on List View in PowerShell. That works just like ISE cmdlet list menu.

1

u/YT-Deliveries Mar 09 '23

Ah, good to know. Thanks for the update :)

1

u/MrRad5000 Mar 19 '23

Hi this was working until today Im getting Invoke-restmethod : 502 Bad gateway nginx

2

u/_ouromoros Mar 19 '23

I just checked that it is working fine on my pc. Is it still happening consistently for you? Since the error is a server error, it might be network issue or something wrong with the openai's API...

1

u/MrRad5000 Mar 19 '23

Thanks for the reply and narrowing it down to my end. Ill try generating a new key tomorrow.

Ive been using powerGPT for about a week and while it has limitations its extremely helpful to get you started in the right direction. especially in learning powershell when a lot of search results send you to basic microsoft cmdlet pages.

3

u/Bassie_c Mar 08 '23

I think if AI keeps improving like this, r/itsaunixsystem can retire in a couple years πŸ˜‚

User: Enhance image!

Mainframe: *Generates an image with more pixels πŸ˜‚*

1

u/teffhk Mar 08 '23

Awesome!

1

u/old-dirty-olorin Mar 09 '23 edited Mar 09 '23

Anyone tested this with diskpart or DISM or robocopy modules? The module should run fine in a winPE environment?I will make a win10 VM tomorrow to test this.

1

u/deadflamingo Mar 09 '23

How does this compare to the Codex CLI for powershell?

1

u/g3n3 Mar 10 '23

Checkout powershellai by Doug Finke. Maybe you all can join forces.