简体   繁体   中英

Powershell: Uninstall Software that has prompts for user input

Does anybody know if it is possible to uninstall software through PowerShell if it prompts for user input? I have multiple scripts that can uninstall just about anything, except for the one software that I need it to uninstall. I'm specifically trying to uninstall PDFForge toolbar which is installed when downloading PDFCreator.

None of the scripts that I've written fail, they just hang when run, I believe because the uninstall process is asking for user input to proceed.

PowerShell isn't going to interact with the prompts... you can't just tell it to click "Next" in an executable because it can't see it.

You can send keys to it though. You're really just using COM objects.

So, first, get your process ID by setting a variable that contains an array, the data for which is defined by the name of your process. Let's say your process is called, "Uninstall" and the process is ALREADY RUNNING:

$a = Get-Process | ?{$_.Name -eq "Uninstall"}

Start the COM:

$wshell = New-Object -ComObject wscript.shell;

Bring the uninstallation program with this process ID to the front so we can send it keystrokes:

$wshell.AppActivate($a.id)

Give it a few seconds to bring that window forward. I chose 5, but if your machine isn't stressed, 2 is probably enough:

Start-Sleep 5

Now start telling it what keys you want to send. The syntax here is this: whatever is in the () is what will be sent. The position in the single-quote is the keystroke to send, after the comma is how long you want it to wait before proceeding. Assuming the first screen is "Next" you can send your first command by telling PowerShell to send the ENTER key and wait 5 seconds:

$wshell.SendKeys('~',5)

The wait function is optional, but for your purposes, you're definitely going to want it. (If you don't want it $wshell.SendKeys('~') would send the ENTER key and immediately move to the next command.)

Walk through the uninstallation yourself manually, using all keystrokes, and for any keystroke you send, pay attention to how long it takes before the next part of uninstallation is loaded, and wait longer than that with your script (eg if it processes your ENTER key instantaneously, I'd have it wait 3 or 5 seconds before it sends the next command. If it takes 5 seconds to load, I'd tell it to wait 10 seconds before sending the next command).

Letters are letters, and numbers are numbers. Most non-commands are just assigned to their keys (meaning if you want to type "K" your command would just be $wshell.SendKeys('K') ) You can get the rundown for the specific keys here: https://msdn.microsoft.com/en-us/library/office/aa202943(v=office.10).aspx .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM