PowerWut is a PowerShell module designed to interact with OpenAI’s models to analyze your PowerShell command history and answer custom queries. It securely manages your API key using the Windows Credential Vault and provides several helper functions for model management, secret storage, and intelligent AI querying.
The PowerWut module simplifies the process of querying OpenAI models from PowerShell. It performs the following tasks:
- Module Installation & Import: Checks for and installs the
Microsoft.PowerShell.SecretManagementandMicrosoft.PowerShell.SecretStoremodules if necessary. - Secret Storage: Securely stores and retrieves your OpenAI API key using the Windows Credential Vault.
- Model Management: Lists available OpenAI models and allows you to switch between them.
- Command History Analysis: Captures recent PowerShell commands as context to aid in error analysis and troubleshooting.
- Query Execution: Sends the command history or custom queries to OpenAI’s API and returns the AI’s response.
- Automated Dependency Management: Automatically installs required secret management modules if not already present.
- Secure API Key Storage: Uses Windows Credential Vault to safely store your OpenAI API key.
- Dynamic AI Model Selection: Allows listing and switching between available AI models.
- Contextual Analysis: Aggregates your recent command history to provide context to the AI model.
- Verbose Mode: Offers detailed debugging output including API request payloads and responses.
- Help System: Includes a built-in help function that explains usage, parameters, and examples.
- PowerShell 5.1 or later (or PowerShell Core)
- Internet Access to reach the OpenAI API endpoints
- OpenAI API Key – Obtain one from OpenAI's API Keys Page
-
Download the Module:
- Save the provided PowerShell module as
PowerWut.psm1.
- Save the provided PowerShell module as
-
Import the Module:
- Open a PowerShell terminal and import the module:
Import-Module .\PowerWut.psm1 - Alternatively, you can add the module's folder to your
$env:PSModulePathfor easier future access.
- Open a PowerShell terminal and import the module:
-
Module Dependencies:
- The module automatically checks for, installs (if needed), and imports the following modules:
Microsoft.PowerShell.SecretManagementMicrosoft.PowerShell.SecretStore
- It also registers a secret vault named WutKeyVault for secure API key storage.
- The module automatically checks for, installs (if needed), and imports the following modules:
-
Persisting the Module:
- To load PowerWut automatically in future sessions, consider adding the import command to your PowerShell profile.
Before running any AI queries, set your OpenAI API key:
-
Interactive Setup:
- On first use, if no API key is found, the module will prompt:
No API Key found. Please enter your OpenAI API Key. - Enter your key when prompted.
- On first use, if no API key is found, the module will prompt:
-
Manually Set the API Key:
- Use the provided function:
Set-OpenAIAPIKey -ApiKey "your_openai_api_key"
- Use the provided function:
-
List Available Models:
- Run:
wut -m - This displays the current model and lists available AI models (after applying filtering rules).
- Run:
-
Change the Current Model:
- Use the
-smparameter:wut -sm "gpt-4-turbo"
- The module sets the new model as the current model for subsequent queries.
- Use the
-
Analyze Recent Command History:
- Run a query without specifying a custom query:
wut
- This sends the last 10 commands (default) as context to the AI model.
- Run a query without specifying a custom query:
-
Custom Query with Command History:
- Include both command history and a custom query:
wut -c 20 -q "Explain the last 20 commands"
- The
-cparameter adjusts how many recent commands to include.
- Include both command history and a custom query:
-
Direct Query Without History:
- To send a direct query without context, use a context length of zero:
wut -q "What is Azure?" -c 0
- To send a direct query without context, use a context length of zero:
-
Verbose Mode:
- Add
-vto see detailed API request and response information:wut -q "What is PowerShell?" -v
- Add
- Display the built-in help message by running:
wut -?