• Stars
    star
    142
  • Rank 258,495 (Top 6 %)
  • Language
    PowerShell
  • Created over 1 year ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Do anything in Windows PowerShell using natural language

PowerGPT

Writing shell scripts can be difficult and boring. This module will help you do any common task/batch processing in Windows terminal in seconds even if you know nothing about PowerShell.

Just type and run PowerGPT <your task> like this and the GPT model will do the rest for you:

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

Even for PowerShell experts, typing a few words and let the AI model do the dirty work certainly sounds like a better way to go ;)

The script is implemented purely in PowerSell and packaged as a PowerShell module.

ChangeLog

[1.0.3] 2023-03-19

  • Add support for -Chat operation that uses gpt-3.5-turbo model. Pros: cheaper and faster. Cons: can't produce suggestions like the text-davinnci-3 model.
  • Fix Invoke-RestMethod encoding so that now most languages would work.
$ PowerGPT -Chat "列出所有文件"
我们使用`Get-ChildItem`来列出所有文件。
\```
Get-ChildItem -Path "C:\" -Recurse -File
\```
这将列出C盘下所有文件,包括子文件夹中的文件。如果要列出当前文件夹下的所有文件,可以将`-Path`参数设置为`.`。

Will execute script:
-----

Get-ChildItem -Path "C:\" -Recurse -File

-----

Installation

Install-Module PowerGPT

You will need an OpenAI API key to use this module. The command will ask input for the API key on first use. To reset the API key, use -ResetConfig option.

Usage

Basic Usage:

$ PowerGPT "list all files in current folder with created date"
Will execute script:
-----
Get-ChildItem | Select-Object Name, CreationTime
-----
continue?([y]es, [n]o): 

$ PowerGPT "extract compressed.tar.gz"
Will execute script:
-----
# Extract compressed.tar.gz in Windows using PowerShell
# First, check if the tar command is available
if (!(Get-Command tar -ErrorAction SilentlyContinue)) {
    # If not, install the tar command
    Invoke-WebRequest -Uri "http://gnuwin32.sourceforge.net/downlinks/tar.exe.zip" -OutFile "tar.exe.zip"
    Expand-Archive -Path "tar.exe.zip" -DestinationPath "$env:ProgramFiles\GnuWin32"
    # Add the tar command to the PATH
    $env:Path += ";$env:ProgramFiles\GnuWin32"
}
# Extract the compressed.tar.gz file
tar -xvzf compressed.tar.gz
-----
continue?([y]es, [n]o):
$ PowerGPT "print the first line of all the files that begin with poet_ in current folder"
Will execute script:
-----
Get-ChildItem -Path . -Filter "poet_*" | ForEach-Object {Get-Content $_.FullName | Select-Object -First 1}
-----
continue?([y]es, [n]o):

For complex task, the tool will behave smartly and provide choices for user:

$ PowerGPT "print first lines and last lines for each file in current folder"
The description is too vague, do you mean:
[0] For each file in current directory, print the first line and then print the last line of the file.
[1] For each file in current directory, print the first line of the file. After that, for each file, print the last line of the file.
Choose one description that matches your task: : 1
Will execute script:
-----
Get-ChildItem | ForEach-Object {
    $file = $_.FullName
    Write-Host "First line of $file:"
    Get-Content $file -TotalCount 1
}
Get-ChildItem | ForEach-Object {
    $file = $_.FullName
    Write-Host "Last line of $file:"
    Get-Content $file -Tail 1
}
-----
continue?([y]es, [n]o)

It's also possible for it to write script using common library in other languages:

$ PowerGPT "retrieve AzureDevops artifact for a build" -ShellVariant C#
The description is a little vague, do you mean:
[0] Use AzureDevops REST API to retrieve the artifact for a build.
[1] Use AzureDevops SDK to retrieve the artifact for a build.
Choose one description that matches your task, or [n]o: 0
using System;
using System.Net.Http;
using System.Threading.Tasks;

class Program
{
    static async Task Main(string[] args)
    {
        var organization = "your_organization";
        var project = "your_project";
        var buildId = "your_build_id";
        var token = "your_token";

        var client = new HttpClient();
        client.DefaultRequestHeaders.Add("Authorization", $"Bearer {token}");

        var url = $"https://dev.azure.com/{organization}/{project}/_apis/build/builds/{buildId}/artifacts?api-version=5.1";
        var response = await client.GetAsync(url);
        var content = await response.Content.ReadAsStringAsync();
        Console.WriteLine(content);
    }
}