Skip to content

Instantly share code, notes, and snippets.

@jimbrig
Forked from ruvnet/registry-optimizer.md
Created April 15, 2025 16:02
Show Gist options
  • Save jimbrig/d72de977aa62cabb29cad7f0389e2433 to your computer and use it in GitHub Desktop.
Save jimbrig/d72de977aa62cabb29cad7f0389e2433 to your computer and use it in GitHub Desktop.
Ai powered windows 11 registry optimization

Comprehensive Guide to Review and Optimize Windows Registry on Windows 11 with AI Support

This guide provides a comprehensive PowerShell script that allows you to backup, restore, analyze, and optimize your Windows Registry. Additionally, it integrates the OpenAI GPT-4 API to provide AI-powered analysis of your registry settings.

Table of Contents

  1. Prepare Your System
  2. Set Up OpenAI API Access
  3. Enable Script Execution
  4. The Enhanced Registry Optimization Script
  5. Save the Script
  6. Run the Script
  7. Understand the Optimization Tweaks
  8. Post-Optimization Steps
  9. Additional Recommendations

Disclaimer: Modifying the Windows Registry can have significant impacts on your system. Incorrect changes may lead to system instability or failure. Always back up your registry and create a system restore point before proceeding. This script is provided for educational purposes and should be used at your own risk.



1. Prepare Your System

Before making any changes to your registry, it's crucial to back it up and create a system restore point.

  • Backup the Registry:

    1. Press Win + R, type regedit, and press Enter to open the Registry Editor.
    2. In the Registry Editor, click on File > Export.
    3. Choose a location to save the backup, name the file (e.g., RegistryBackup.reg), ensure All is selected under Export range, and click Save.
  • Create a System Restore Point:

    1. Press Win + S, type Create a restore point, and press Enter.
    2. In the System Properties window, under the System Protection tab, click Create.
    3. Follow the prompts to create a restore point.

2. Set Up OpenAI API Access

To enable AI-powered analysis using OpenAI's GPT-4 model, you'll need to obtain an API key.

  1. Create an OpenAI Account:

  2. Obtain an API Key:

    • After logging in, navigate to the API section.
    • Click on Create new secret key.
    • Copy and securely store the API key as it will be shown only once.
  3. Store the API Key Securely:

    • It's recommended to store the API key in an environment variable or a secure file.
    • Using Environment Variable:
      • Open PowerShell and execute:
        [Environment]::SetEnvironmentVariable("OPENAI_API_KEY", "your-api-key-here", "User")
      • Replace "your-api-key-here" with your actual API key.
    • Alternatively, Using a Secure File:
      • Create a text file (e.g., C:\Scripts\OpenAI_API_Key.txt) and paste your API key.
      • Ensure the file has restricted permissions to prevent unauthorized access.

3. Enable Script Execution

To run PowerShell scripts, adjust the execution policy:

  1. Open PowerShell as Administrator:

    • Press Win + X and select Windows Terminal (Admin) or PowerShell (Admin).
  2. Set Execution Policy:

    Set-ExecutionPolicy RemoteSigned -Scope CurrentUser
    • Type Y and press Enter to confirm.

4. The Enhanced Registry Optimization Script

Below is an enhanced PowerShell script named RegistryManager.ps1 that includes:

  • Command-Line Menu Options:

    • Backup Registry
    • Restore Registry
    • Analyze Registry
    • Optimize Registry
    • AI-Powered Analysis
    • Exit
  • AI Integration:

    • Utilizes OpenAI's GPT-4 API for intelligent analysis and recommendations.

Script: RegistryManager.ps1

# RegistryManager.ps1
# Comprehensive script to backup, restore, analyze, and optimize Windows Registry with AI support.

# ===========================
# Configuration
# ===========================

# Path to store registry backups
$BackupDirectory = "$env:USERPROFILE\RegistryBackups"
if (-Not (Test-Path $BackupDirectory)) {
    New-Item -Path $BackupDirectory -ItemType Directory | Out-Null
}

# Path to store AI analysis report
$AIReportPath = "$env:USERPROFILE\RegistryBackups\AI_Registry_Analysis_$((Get-Date).ToString('yyyyMMddHHmmss')).txt"

# ===========================
# Functions
# ===========================

# Function to prompt for confirmation
function Confirm-Action {
    param (
        [string]$Message
    )
    do {
        $choice = Read-Host "$Message (Y/N)"
    } while ($choice -notin @('Y','y','N','n'))
    return $choice -eq 'Y' -or $choice -eq 'y'
}

# Function to backup the entire registry
function Backup-FullRegistry {
    $timestamp = Get-Date -Format "yyyyMMddHHmmss"
    $backupPath = "$BackupDirectory\FullRegistryBackup_$timestamp.reg"
    try {
        reg export HKLM $backupPath /y
        reg export HKCU "$BackupDirectory\HKEY_CURRENT_USER_$timestamp.reg" /y
        Write-Output "Full registry backup created at:"
        Write-Output "  HKLM: $backupPath"
        Write-Output "  HKCU: $BackupDirectory\HKEY_CURRENT_USER_$timestamp.reg"
    } catch {
        Write-Error "Failed to backup the registry: $_"
    }
}

# Function to restore the registry from a backup
function Restore-Registry {
    $backupFiles = Get-ChildItem -Path $BackupDirectory -Filter "*.reg" | Select-Object Name
    if ($backupFiles.Count -eq 0) {
        Write-Output "No backup files found in $BackupDirectory."
        return
    }

    Write-Output "Available Backup Files:"
    for ($i=0; $i -lt $backupFiles.Count; $i++) {
        Write-Output "$($i + 1): $($backupFiles[$i].Name)"
    }

    $selection = Read-Host "Enter the number of the backup file you want to restore"
    if ($selection -match '^\d+$' -and $selection -ge 1 -and $selection -le $backupFiles.Count) {
        $selectedFile = $backupFiles[$selection - 1].FullName
        if (Confirm-Action "Are you sure you want to restore the registry from $($backupFiles[$selection - 1].Name)? This may require a system restart.") {
            try {
                reg import $selectedFile
                Write-Output "Registry restored from $selectedFile"
            } catch {
                Write-Error "Failed to restore the registry: $_"
            }
        } else {
            Write-Output "Restore operation cancelled."
        }
    } else {
        Write-Output "Invalid selection."
    }
}

# Function to analyze the registry for common issues
function Analyze-Registry {
    Write-Output "Analyzing registry for common optimization opportunities..."

    # Define registry paths and values to analyze
    $registryAnalysis = @(
        @{
            Name = "Disable Startup Delay"
            Key = "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Serialize"
            ValueName = "StartupDelayInMSec"
        },
        @{
            Name = "Disable Animation"
            Key = "HKCU:\Control Panel\Desktop\WindowMetrics"
            ValueName = "MinAnimate"
        },
        @{
            Name = "Enable Fast Boot"
            Key = "HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager\Power"
            ValueName = "HiberbootEnabled"
        },
        @{
            Name = "Increase Menu Show Delay"
            Key = "HKCU:\Control Panel\Desktop\WindowMetrics"
            ValueName = "MenuShowDelay"
        },
        @{
            Name = "Disable Telemetry"
            Key = "HKLM:\SOFTWARE\Policies\Microsoft\Windows\DataCollection"
            ValueName = "AllowTelemetry"
        }
    )

    foreach ($item in $registryAnalysis) {
        Write-Output "`n$item.Name:"
        if (Test-Path $item.Key) {
            $currentValue = (Get-ItemProperty -Path $item.Key -Name $item.ValueName -ErrorAction SilentlyContinue).$($item.ValueName)
            if ($null -ne $currentValue) {
                Write-Output "  Current Value of $($item.ValueName): $currentValue"
            } else {
                Write-Output "  $($item.ValueName) does not exist."
            }
        } else {
            Write-Output "  Registry key $($item.Key) does not exist."
        }
    }

    Write-Output "`nAnalysis Completed."
}

# Function to optimize registry settings
function Optimize-Registry {
    Write-Output "Starting Windows Registry Optimization..."

    # Define registry tweaks
    $registryTweaks = @(
        @{
            Name = "Disable Startup Delay"
            Key = "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Serialize"
            ValueName = "StartupDelayInMSec"
            NewValue = 0
            Type = "DWord"
            Description = "Reduces startup delay to speed up boot time."
        },
        @{
            Name = "Disable Animation"
            Key = "HKCU:\Control Panel\Desktop\WindowMetrics"
            ValueName = "MinAnimate"
            NewValue = 0
            Type = "DWord"
            Description = "Disables window animations for better performance."
        },
        @{
            Name = "Enable Fast Boot"
            Key = "HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager\Power"
            ValueName = "HiberbootEnabled"
            NewValue = 1
            Type = "DWord"
            Description = "Enables fast startup to reduce boot time."
        },
        @{
            Name = "Increase Menu Show Delay"
            Key = "HKCU:\Control Panel\Desktop\WindowMetrics"
            ValueName = "MenuShowDelay"
            NewValue = 0
            Type = "DWord"
            Description = "Sets menu show delay to minimum for faster menu appearance."
        },
        @{
            Name = "Disable Telemetry"
            Key = "HKLM:\SOFTWARE\Policies\Microsoft\Windows\DataCollection"
            ValueName = "AllowTelemetry"
            NewValue = 0
            Type = "DWord"
            Description = "Disables Windows telemetry for enhanced privacy and performance."
        }
    )

    foreach ($tweak in $registryTweaks) {
        Write-Output "`nProcessing: $($tweak.Name)"
        try {
            # Backup the registry key before making changes
            $timestamp = Get-Date -Format "yyyyMMddHHmmss"
            $safeName = $tweak.Name -replace ' ', '_'
            $backupPath = "$BackupDirectory\RegistryBackup_$safeName_$timestamp.reg"
            reg export $tweak.Key $backupPath /y
            Write-Output "Backup of $($tweak.Key) created at $backupPath"

            # Ensure the registry key exists
            if (-Not (Test-Path $tweak.Key)) {
                Write-Warning "Registry key $($tweak.Key) does not exist. Creating it."
                New-Item -Path $tweak.Key -Force | Out-Null
            }

            # Get current value
            $currentValue = (Get-ItemProperty -Path $tweak.Key -Name $tweak.ValueName -ErrorAction SilentlyContinue).$($tweak.ValueName)
            if ($null -ne $currentValue) {
                Write-Output "Current value of $($tweak.ValueName): $currentValue"
            } else {
                Write-Output "$($tweak.ValueName) does not exist. It will be created."
            }

            # Confirm with user before making changes
            if (Confirm-Action "Do you want to change $($tweak.ValueName) to $($tweak.NewValue)?") {
                Set-ItemProperty -Path $tweak.Key -Name $tweak.ValueName -Value $tweak.NewValue -Type $tweak.Type
                Write-Output "$($tweak.ValueName) set to $($tweak.NewValue)"
            } else {
                Write-Output "Skipped changing $($tweak.ValueName)"
            }
        } catch {
            Write-Error "Error processing $($tweak.Name): $_"
        }
    }

    Write-Output "`nRegistry Optimization Completed."
}

# Function to perform AI-powered analysis using OpenAI GPT-4
function AI-Powered-Analysis {
    Write-Output "Starting AI-Powered Registry Analysis..."

    # Retrieve OpenAI API Key
    $apiKey = $env:OPENAI_API_KEY
    if (-Not $apiKey) {
        # Attempt to read from a secure file
        $apiKeyPath = "C:\Scripts\OpenAI_API_Key.txt"
        if (Test-Path $apiKeyPath) {
            $apiKey = Get-Content -Path $apiKeyPath -Raw
        } else {
            Write-Error "OpenAI API key not found. Please set the OPENAI_API_KEY environment variable or place the key in C:\Scripts\OpenAI_API_Key.txt"
            return
        }
    }

    # Collect registry data for analysis
    $registryData = ""
    $registryKeys = @(
        "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Serialize",
        "HKCU:\Control Panel\Desktop\WindowMetrics",
        "HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager\Power",
        "HKLM:\SOFTWARE\Policies\Microsoft\Windows\DataCollection"
    )

    foreach ($key in $registryKeys) {
        if (Test-Path $key) {
            $properties = Get-ItemProperty -Path $key
            $registryData += "`n$key`n"
            foreach ($prop in $properties.PSObject.Properties) {
                $registryData += "$($prop.Name) = $($prop.Value)`n"
            }
        } else {
            $registryData += "`n$key does not exist.`n"
        }
    }

    # Prepare the prompt for GPT-4
    $prompt = @"
You are an expert system optimizer. Analyze the following Windows Registry settings and provide recommendations for improving performance, energy efficiency, security, and overall system stability. Explain each recommendation in detail.

Registry Settings:
$registryData

Provide your analysis below.
"@

    # Prepare the API request
    $requestBody = @{
        model = "gpt-4"
        prompt = $prompt
        max_tokens = 1500
        temperature = 0.5
    } | ConvertTo-Json

    # Send the request to OpenAI API
    try {
        $response = Invoke-RestMethod -Uri "https://api.openai.com/v1/chat/completions" `
                                      -Method Post `
                                      -Headers @{
                                          "Content-Type"  = "application/json"
                                          "Authorization" = "Bearer $apiKey"
                                      } `
                                      -Body (${
                                          @{
                                              model = "gpt-4"
                                              messages = @(@{role = "system"; content = "You are ChatGPT, a large language model trained by OpenAI."},
                                                           @{role = "user"; content = $prompt})
                                              max_tokens = 1500
                                              temperature = 0.5
                                          } | ConvertTo-Json
                                      })

        $aiResponse = $response.choices[0].message.content
        # Save the AI analysis to a file
        $aiResponse | Out-File -FilePath $AIReportPath -Encoding UTF8
        Write-Output "AI analysis completed. Report saved at $AIReportPath"
    } catch {
        Write-Error "Failed to communicate with OpenAI API: $_"
    }
}

# Function to display the menu and handle user input
function Show-Menu {
    Clear-Host
    Write-Output "=============================="
    Write-Output "   Windows Registry Manager"
    Write-Output "=============================="
    Write-Output "1. Backup Registry"
    Write-Output "2. Restore Registry"
    Write-Output "3. Analyze Registry"
    Write-Output "4. Optimize Registry"
    Write-Output "5. AI-Powered Analysis"
    Write-Output "6. Exit"
}

# ===========================
# Main Execution Loop
# ===========================

do {
    Show-Menu
    $choice = Read-Host "Enter your choice (1-6)"
    switch ($choice) {
        '1' {
            Backup-FullRegistry
            Pause
        }
        '2' {
            Restore-Registry
            Pause
        }
        '3' {
            Analyze-Registry
            Pause
        }
        '4' {
            Optimize-Registry
            Pause
        }
        '5' {
            AI-Powered-Analysis
            Pause
        }
        '6' {
            Write-Output "Exiting Registry Manager."
            break
        }
        default {
            Write-Output "Invalid choice. Please select a valid option."
            Pause
        }
    }
} while ($true)

5. Save the Script

  1. Open a Text Editor:

    • Use Notepad, Visual Studio Code, or any other text editor of your choice.
  2. Copy and Paste the Script:

    • Copy the entire script provided above and paste it into the text editor.
  3. Save the File:

    • Save the file as RegistryManager.ps1 in a directory of your choice, e.g., C:\Scripts\RegistryManager.ps1.
  4. Ensure Correct File Extension:

    • Make sure the file is saved with a .ps1 extension, not .txt.

6. Run the Script

  1. Open PowerShell as Administrator:

    • Press Win + X and select Windows Terminal (Admin) or PowerShell (Admin).
  2. Navigate to the Script Location:

    cd C:\Scripts
  3. Execute the Script:

    .\RegistryManager.ps1
  4. Interact with the Menu:

    • Upon running, you'll see a menu with the following options:
      ==============================
         Windows Registry Manager
      ==============================
      1. Backup Registry
      2. Restore Registry
      3. Analyze Registry
      4. Optimize Registry
      5. AI-Powered Analysis
      6. Exit
      
    • Enter the number corresponding to the action you wish to perform and follow the prompts.

7. Understand the Optimization Tweaks

The script includes several registry tweaks aimed at improving performance, energy efficiency, security, and overall system stability:

  1. Disable Startup Delay:

    • Path: HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Serialize
    • Value: StartupDelayInMSec set to 0
    • Purpose: Reduces the delay during startup to speed up boot time.
  2. Disable Animation:

    • Path: HKCU:\Control Panel\Desktop\WindowMetrics
    • Value: MinAnimate set to 0
    • Purpose: Disables window animations for a snappier user interface.
  3. Enable Fast Boot:

    • Path: HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager\Power
    • Value: HiberbootEnabled set to 1
    • Purpose: Enables fast startup to reduce boot time.
  4. Increase Menu Show Delay:

    • Path: HKCU:\Control Panel\Desktop\WindowMetrics
    • Value: MenuShowDelay set to 0
    • Purpose: Sets the delay before menus appear to minimum for faster access.
  5. Disable Telemetry:

    • Path: HKLM:\SOFTWARE\Policies\Microsoft\Windows\DataCollection
    • Value: AllowTelemetry set to 0
    • Purpose: Disables Windows telemetry to enhance privacy and potentially improve performance.

8. Post-Optimization Steps

  • Verify System Stability:

    • After performing optimizations and restarting (if prompted), monitor your system to ensure that all applications and features are functioning correctly.
  • Review AI Analysis Report:

    • If you performed an AI-powered analysis, review the report saved at the specified location (e.g., C:\Users\YourName\RegistryBackups\AI_Registry_Analysis_YYYYMMDDHHMMSS.txt) for additional recommendations.
  • Restore Registry (If Needed):

    • If you encounter issues after making changes:
      1. Open Registry Manager (RegistryManager.ps1).
      2. Select the Restore Registry option.
      3. Choose the appropriate backup file to restore.
  • Delete Old Backup Files:

    • Once you're confident that your system is stable, you can delete older backup .reg files in the C:\Users\YourName\RegistryBackups directory to free up space.

9. Additional Recommendations

  • Regular Maintenance:

    • Regularly review and clean your registry using trusted tools or scripts to maintain system performance.
  • Use Reliable Tools:

    • Consider using reputable registry optimization tools that offer AI-based analysis and optimization for more comprehensive results.
  • Stay Updated:

    • Keep your Windows 11 system updated to benefit from the latest performance and security enhancements from Microsoft.
  • Secure Your API Key:

    • Ensure that your OpenAI API key is stored securely and not exposed in scripts or shared locations.
  • Understand the Changes:

    • Familiarize yourself with each registry tweak to understand how it affects your system. If unsure, consult official Microsoft documentation or seek professional assistance.

Caution: Always ensure that any script or tool you use is from a trusted source and understand the changes it will make to your system. When in doubt, consult with a professional or refer to official Microsoft documentation.


Happy Optimizing! 🚀

# RegistryManager.ps1
# Comprehensive script to backup, restore, analyze, and optimize Windows Registry with AI support.
# ===========================
# Configuration
# ===========================
# Path to store registry backups
$BackupDirectory = "$env:USERPROFILE\RegistryBackups"
if (-Not (Test-Path $BackupDirectory)) {
New-Item -Path $BackupDirectory -ItemType Directory | Out-Null
}
# Path to store AI analysis report
$AIReportPath = "$env:USERPROFILE\RegistryBackups\AI_Registry_Analysis_$((Get-Date).ToString('yyyyMMddHHmmss')).txt"
# ===========================
# Functions
# ===========================
# Function to prompt for confirmation
function Confirm-Action {
param (
[string]$Message
)
do {
$choice = Read-Host "$Message (Y/N)"
} while ($choice -notin @('Y','y','N','n'))
return $choice -eq 'Y' -or $choice -eq 'y'
}
# Function to backup the entire registry
function Backup-FullRegistry {
$timestamp = Get-Date -Format "yyyyMMddHHmmss"
$backupPath = "$BackupDirectory\FullRegistryBackup_$timestamp.reg"
try {
reg export HKLM $backupPath /y
reg export HKCU "$BackupDirectory\HKEY_CURRENT_USER_$timestamp.reg" /y
Write-Output "Full registry backup created at:"
Write-Output " HKLM: $backupPath"
Write-Output " HKCU: $BackupDirectory\HKEY_CURRENT_USER_$timestamp.reg"
} catch {
Write-Error "Failed to backup the registry: $_"
}
}
# Function to restore the registry from a backup
function Restore-Registry {
$backupFiles = Get-ChildItem -Path $BackupDirectory -Filter "*.reg" | Select-Object Name
if ($backupFiles.Count -eq 0) {
Write-Output "No backup files found in $BackupDirectory."
return
}
Write-Output "Available Backup Files:"
for ($i=0; $i -lt $backupFiles.Count; $i++) {
Write-Output "$($i + 1): $($backupFiles[$i].Name)"
}
$selection = Read-Host "Enter the number of the backup file you want to restore"
if ($selection -match '^\d+$' -and $selection -ge 1 -and $selection -le $backupFiles.Count) {
$selectedFile = $backupFiles[$selection - 1].FullName
if (Confirm-Action "Are you sure you want to restore the registry from $($backupFiles[$selection - 1].Name)? This may require a system restart.") {
try {
reg import $selectedFile
Write-Output "Registry restored from $selectedFile"
} catch {
Write-Error "Failed to restore the registry: $_"
}
} else {
Write-Output "Restore operation cancelled."
}
} else {
Write-Output "Invalid selection."
}
}
# Function to analyze the registry for common issues
function Analyze-Registry {
Write-Output "Analyzing registry for common optimization opportunities..."
# Define registry paths and values to analyze
$registryAnalysis = @(
@{
Name = "Disable Startup Delay"
Key = "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Serialize"
ValueName = "StartupDelayInMSec"
},
@{
Name = "Disable Animation"
Key = "HKCU:\Control Panel\Desktop\WindowMetrics"
ValueName = "MinAnimate"
},
@{
Name = "Enable Fast Boot"
Key = "HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager\Power"
ValueName = "HiberbootEnabled"
},
@{
Name = "Increase Menu Show Delay"
Key = "HKCU:\Control Panel\Desktop\WindowMetrics"
ValueName = "MenuShowDelay"
},
@{
Name = "Disable Telemetry"
Key = "HKLM:\SOFTWARE\Policies\Microsoft\Windows\DataCollection"
ValueName = "AllowTelemetry"
}
)
foreach ($item in $registryAnalysis) {
Write-Output "`n$item.Name:"
if (Test-Path $item.Key) {
$currentValue = (Get-ItemProperty -Path $item.Key -Name $item.ValueName -ErrorAction SilentlyContinue).$($item.ValueName)
if ($null -ne $currentValue) {
Write-Output " Current Value of $($item.ValueName): $currentValue"
} else {
Write-Output " $($item.ValueName) does not exist."
}
} else {
Write-Output " Registry key $($item.Key) does not exist."
}
}
Write-Output "`nAnalysis Completed."
}
# Function to optimize registry settings
function Optimize-Registry {
Write-Output "Starting Windows Registry Optimization..."
# Define registry tweaks
$registryTweaks = @(
@{
Name = "Disable Startup Delay"
Key = "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Serialize"
ValueName = "StartupDelayInMSec"
NewValue = 0
Type = "DWord"
Description = "Reduces startup delay to speed up boot time."
},
@{
Name = "Disable Animation"
Key = "HKCU:\Control Panel\Desktop\WindowMetrics"
ValueName = "MinAnimate"
NewValue = 0
Type = "DWord"
Description = "Disables window animations for better performance."
},
@{
Name = "Enable Fast Boot"
Key = "HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager\Power"
ValueName = "HiberbootEnabled"
NewValue = 1
Type = "DWord"
Description = "Enables fast startup to reduce boot time."
},
@{
Name = "Increase Menu Show Delay"
Key = "HKCU:\Control Panel\Desktop\WindowMetrics"
ValueName = "MenuShowDelay"
NewValue = 0
Type = "DWord"
Description = "Sets menu show delay to minimum for faster menu appearance."
},
@{
Name = "Disable Telemetry"
Key = "HKLM:\SOFTWARE\Policies\Microsoft\Windows\DataCollection"
ValueName = "AllowTelemetry"
NewValue = 0
Type = "DWord"
Description = "Disables Windows telemetry for enhanced privacy and performance."
}
)
foreach ($tweak in $registryTweaks) {
Write-Output "`nProcessing: $($tweak.Name)"
try {
# Backup the registry key before making changes
$timestamp = Get-Date -Format "yyyyMMddHHmmss"
$safeName = $tweak.Name -replace ' ', '_'
$backupPath = "$BackupDirectory\RegistryBackup_$safeName_$timestamp.reg"
reg export $tweak.Key $backupPath /y
Write-Output "Backup of $($tweak.Key) created at $backupPath"
# Ensure the registry key exists
if (-Not (Test-Path $tweak.Key)) {
Write-Warning "Registry key $($tweak.Key) does not exist. Creating it."
New-Item -Path $tweak.Key -Force | Out-Null
}
# Get current value
$currentValue = (Get-ItemProperty -Path $tweak.Key -Name $tweak.ValueName -ErrorAction SilentlyContinue).$($tweak.ValueName)
if ($null -ne $currentValue) {
Write-Output "Current value of $($tweak.ValueName): $currentValue"
} else {
Write-Output "$($tweak.ValueName) does not exist. It will be created."
}
# Confirm with user before making changes
if (Confirm-Action "Do you want to change $($tweak.ValueName) to $($tweak.NewValue)?") {
Set-ItemProperty -Path $tweak.Key -Name $tweak.ValueName -Value $tweak.NewValue -Type $tweak.Type
Write-Output "$($tweak.ValueName) set to $($tweak.NewValue)"
} else {
Write-Output "Skipped changing $($tweak.ValueName)"
}
} catch {
Write-Error "Error processing $($tweak.Name): $_"
}
}
Write-Output "`nRegistry Optimization Completed."
}
# Function to perform AI-powered analysis using OpenAI GPT-4
function AI-Powered-Analysis {
Write-Output "Starting AI-Powered Registry Analysis..."
# Retrieve OpenAI API Key
$apiKey = $env:OPENAI_API_KEY
if (-Not $apiKey) {
# Attempt to read from a secure file
$apiKeyPath = "C:\Scripts\OpenAI_API_Key.txt"
if (Test-Path $apiKeyPath) {
$apiKey = Get-Content -Path $apiKeyPath -Raw
} else {
Write-Error "OpenAI API key not found. Please set the OPENAI_API_KEY environment variable or place the key in C:\Scripts\OpenAI_API_Key.txt"
return
}
}
# Collect registry data for analysis
$registryData = ""
$registryKeys = @(
"HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Serialize",
"HKCU:\Control Panel\Desktop\WindowMetrics",
"HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager\Power",
"HKLM:\SOFTWARE\Policies\Microsoft\Windows\DataCollection"
)
foreach ($key in $registryKeys) {
if (Test-Path $key) {
$properties = Get-ItemProperty -Path $key
$registryData += "`n$key`n"
foreach ($prop in $properties.PSObject.Properties) {
$registryData += "$($prop.Name) = $($prop.Value)`n"
}
} else {
$registryData += "`n$key does not exist.`n"
}
}
# Prepare the prompt for GPT-4
$prompt = @"
You are an expert system optimizer. Analyze the following Windows Registry settings and provide recommendations for improving performance, energy efficiency, security, and overall system stability. Explain each recommendation in detail.
Registry Settings:
$registryData
Provide your analysis below.
"@
# Prepare the API request
$requestBody = @{
model = "gpt-4"
prompt = $prompt
max_tokens = 1500
temperature = 0.5
} | ConvertTo-Json
# Send the request to OpenAI API
try {
$response = Invoke-RestMethod -Uri "https://api.openai.com/v1/chat/completions" `
-Method Post `
-Headers @{
"Content-Type" = "application/json"
"Authorization" = "Bearer $apiKey"
} `
-Body (${
@{
model = "gpt-4"
messages = @(@{role = "system"; content = "You are ChatGPT, a large language model trained by OpenAI."},
@{role = "user"; content = $prompt})
max_tokens = 1500
temperature = 0.5
} | ConvertTo-Json
})
$aiResponse = $response.choices[0].message.content
# Save the AI analysis to a file
$aiResponse | Out-File -FilePath $AIReportPath -Encoding UTF8
Write-Output "AI analysis completed. Report saved at $AIReportPath"
} catch {
Write-Error "Failed to communicate with OpenAI API: $_"
}
}
# Function to display the menu and handle user input
function Show-Menu {
Clear-Host
Write-Output "=============================="
Write-Output " Windows Registry Manager"
Write-Output "=============================="
Write-Output "1. Backup Registry"
Write-Output "2. Restore Registry"
Write-Output "3. Analyze Registry"
Write-Output "4. Optimize Registry"
Write-Output "5. AI-Powered Analysis"
Write-Output "6. Exit"
}
# ===========================
# Main Execution Loop
# ===========================
do {
Show-Menu
$choice = Read-Host "Enter your choice (1-6)"
switch ($choice) {
'1' {
Backup-FullRegistry
Pause
}
'2' {
Restore-Registry
Pause
}
'3' {
Analyze-Registry
Pause
}
'4' {
Optimize-Registry
Pause
}
'5' {
AI-Powered-Analysis
Pause
}
'6' {
Write-Output "Exiting Registry Manager."
break
}
default {
Write-Output "Invalid choice. Please select a valid option."
Pause
}
}
} while ($true)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment