求助:编写PowerShell脚本向卸载提示框发送TAB和Enter按键以实现IObit Uninstaller静默卸载
Hey Alex, let's break down why your original script isn't working and fix it step by step.
The Problem with Your Original Script
Your current code launches the uninstaller and immediately sends keystrokes—but the uninstall window probably hasn't finished loading yet. Plus, there's no guarantee the uninstall window is the active foreground window when the keys are sent, so the input might go to the wrong app entirely.
Fixed PowerShell Script
Here's a revised version that waits for the uninstall window to load, activates it, and then sends the required keystrokes with proper delays:
# Load required .NET assemblies for window interaction and keystrokes Add-Type -AssemblyName System.Windows.Forms Add-Type -AssemblyName Microsoft.VisualBasic # Launch the uninstaller and keep track of the process $uninstallPath = "C:\Program Files (x86)\IObit\IObit Uninstaller\unins000.exe" $uninstallProcess = Start-Process -FilePath $uninstallPath -PassThru # Wait for the uninstall window to appear (timeout after 30 seconds) $timeoutSeconds = 30 $startTime = Get-Date do { Start-Sleep -Milliseconds 500 $uninstallProcess.Refresh() # Update process info to get latest window title $windowTitle = $uninstallProcess.MainWindowTitle } while ([string]::IsNullOrEmpty($windowTitle) -and ((Get-Date) - $startTime).TotalSeconds -lt $timeoutSeconds) # Check if we timed out waiting for the window if ([string]::IsNullOrEmpty($windowTitle)) { Write-Error "Uninstall window failed to load within $timeoutSeconds seconds. Exiting." exit 1 } # Activate the uninstall window to ensure keystrokes go to the right place [Microsoft.VisualBasic.Interaction]::AppActivate($windowTitle) Start-Sleep -Milliseconds 200 # Give the window time to become active # Send the TAB and ENTER keystrokes with small delays [System.Windows.Forms.SendKeys]::SendWait("{TAB}") Start-Sleep -Milliseconds 100 [System.Windows.Forms.SendKeys]::SendWait("{ENTER}")
Key Improvements Explained
- Assembly Loading: We explicitly load
System.Windows.Forms(forSendKeys) andMicrosoft.VisualBasic(for activating windows)—this ensures the required tools are available. - Window Wait Loop: We wait until the uninstall window has a visible title (meaning it's loaded) before sending any input, with a timeout to avoid hanging indefinitely.
- Window Activation: Using
AppActivateensures the uninstall window is the foreground window, so your keystrokes don't get sent to another app like your browser or terminal. - Small Delays: Short
Start-Sleepcalls give the UI time to respond between actions—UI operations aren't instant, so these delays prevent missed keystrokes.
Bonus: Try Silent Uninstall First (More Reliable!)
Before relying on keystroke simulation, double-check if the uninstaller supports proper silent parameters. Many Inno Setup installers (which unins000.exe often is) use these arguments:
Start-Process -FilePath $uninstallPath -ArgumentList "/verysilent /suppressmsgboxes" -Wait -NoNewWindow
This is way more reliable than simulating keystrokes, so give it a shot first!
内容的提问来源于stack exchange,提问作者Alexandru Hera

