Here is a quick use of OpenAI / ChatGPT to remove Windows processes. If it doesn’t get it right the first time, it usually is a good enough base to only need an edit or 2 for simpler scripts. Not bad for going 2/2 on no edits needed!
PROMPT 1:
Me:
You are a powershell expert, can you write me a script to find all processes running then show me them in a window and I can select which ones I want to delete?
ChatGPT:
Certainly! Here’s a PowerShell script that will retrieve all running processes, display them in a GUI window using Out-GridView
, and then terminate the selected processes:
# Get all running processes
$processes = Get-Process | Select-Object Id, Name, CPU, Path
# Display processes in a GridView for selection
$selectedProcesses = $processes | Out-GridView -Title "Select processes to terminate" -PassThru
# Terminate selected processes
$selectedProcesses | ForEach-Object {
try {
Stop-Process -Id $_.Id -Force
Write-Host "Terminated process: $($_.Name) with ID: $($_.Id)"
} catch {
Write-Warning "Failed to terminate process: $($_.Name) with ID: $($_.Id)"
}
}
Steps:
- The script fetches all running processes and selects a few useful properties like Id, Name, CPU, and Path.
- The processes are then presented in a GridView window using
Out-GridView
. You can select multiple processes by holding down the Ctrl key and clicking on them. - After you click “OK”, the script will attempt to terminate the selected processes.
Note: Ensure you have the required permissions to terminate processes. Also, be cautious about which processes you terminate, as some are essential for the proper functioning of your system.
Review 1:
You can see that it created the first prompt perfectly, and no edits were necessary.
Selected the processes and gave me a GUI to delete the ones I don’t need anymore
PROMPT 2:
ME:
Can you make it so I can select which one I want see beween ram and cpu?
ChatGPT:
Of course! We can add an initial prompt to let you select between viewing processes based on RAM or CPU usage.
Here’s the updated script:
# Get all running processes
$processes = Get-Process | Select-Object Id, Name, CPU, WS
# Prompt the user to choose between CPU and RAM
$choice = $host.ui.PromptForChoice("Selection", "Choose the metric to display top 5 processes:", @("CPU", "RAM"), 0)
# Display the top 5 processes based on the selected metric
if ($choice -eq 0) {
$topProcesses = $processes | Sort-Object CPU -Descending | Select-Object -First 5
} else {
$topProcesses = $processes | Sort-Object WS -Descending | Select-Object -First 5
}
# Display processes in a GridView for selection
$selectedProcesses = $topProcesses | Out-GridView -Title "Select processes to terminate" -PassThru
# Terminate selected processes
$selectedProcesses | ForEach-Object {
try {
Stop-Process -Id $_.Id -Force
Write-Host "Terminated process: $($_.Name) with ID: $($_.Id)"
} catch {
Write-Warning "Failed to terminate process: $($_.Name) with ID: $($_.Id)"
}
}
Now, when you run the script, it will first prompt you to choose between CPU and RAM. After you make your selection, it will display the top 5 processes based on that metric. You can then select which processes you want to terminate.
Review 2:
As you can see this worked exactly how I wanted it to and gave me the option to pick the top 5 highest of ram or cpu usage, then allowed me to delete.
Leave a Reply