1

I am attempting to perform a quiet uninstall of a program using PowerShell. Winget is used to initiate the uninstall process. The UIAutomation features are used press a single button in the UI that confirms the uninstall.

The script works when I execute it line by line, interactively. I've tested it this way multiple times. However, when I try running the script from file, or even just pasting the entire script block into PS, I receive this error:

Exception calling "GetCurrentPattern" with "1" argument(s): "Unsupported Pattern."

I've tried putting start-sleep elements after each action, just to isolate the problem, but that hasn't worked. I've tried running the script in different versions of powershell, I've tried running the script from the system context.

The script should open the program uninstaller and click the "Uninstall" button.

Full script:

#import required assemblies
Add-Type -AssemblyName UIAutomationClient
Add-Type -AssemblyName UIAutomationTypes

#find and run winget (script runs in system context and cant find winget, but directly referencing .exe works)
$winget = "$env:programfiles\WindowsApps\Microsoft.DesktopAppInstaller_*_x64__8wekyb3d8bbwe\winget.exe"
&$winget uninstall dropbox --Disable-Interactivity

#find drobbox uninstaller process
$DBUninstallProc = (Get-Process | Where-Object {$_.ProcessName -eq 'Au_'})
#wait for GUI to load
$null = $DBUninstallProc.WaitForInputIdle(5000)

#get root element
$root = [Windows.Automation.AutomationElement]::RootElement

#Create a PropertyCondition used to grab the Uninstaller Window
$GetDBWindowCondition = New-Object Windows.Automation.PropertyCondition([Windows.Automation.AutomationElement]::ProcessIdProperty, $DBUninstallProc.Id)

#Get the Uninstaller window
$DBUninstallerUI = $root.FindFirst([Windows.Automation.TreeScope]::Children, $GetDBWindowCondition)

#Find the uninstall button
$UninstallButtonCondition = New-Object Windows.Automation.PropertyCondition([Windows.Automation.AutomationElement]::NameProperty, "Uninstall")
$button = $DBUninstallerUI.FindFirst([Windows.Automation.TreeScope]::Descendants, $UninstallButtonCondition)

#Click the uninstall button
$button.GetCurrentPattern([Windows.Automation.InvokePattern]::Pattern) #.Invoke();

2
  • winget uninstall supports a -h (--silent) option. Have you tried it to see if it obviates the need for UI automation? Commented Jun 9, 2023 at 22:21
  • 1
    Unfortunately even when using the -h option, winget displays "Uninstalled Successfully" and then the uninstall wizard for the application pops up. So it looks like winget considers its job done after executing the uninstaller, but some user interaction is still needed in order to start the uninstaller. Commented Jun 10, 2023 at 20:35

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.