Skip to content

Instantly share code, notes, and snippets.

@supercheetah
Created January 12, 2016 00:07
Show Gist options
  • Save supercheetah/ed8251830dfa0122975c to your computer and use it in GitHub Desktop.
Save supercheetah/ed8251830dfa0122975c to your computer and use it in GitHub Desktop.
Look for all programs containing a search string, and uninstall it.
<#
.SYNOPSIS
Looks for installed applications with a search string to be found in either its
name or the publisher name and uninstalls them.
.DESCRIPTION
It takes a search string (-SearchString) that it uses to look through the names
and publishers for the search string, and uninstalls them.
This was primarily made to be used with SCCM.
.PARAMETER SearchString
Mandatory: Yes
Aliases: p, Publisher
This is the string the script will use to search through the list of applications
in both their names and the publisher name. This is case insensitive.
.PARAMETER Silent
Mandatory: No
Aliases: s
Suppress progress output, and a dialog box signifying completion.
.EXAMPLE
Uninstall-Universal.ps1 "java" -s
Look for Java to uninstall silently.
.NOTES
At the moment, it only works with packages installed using an MSI installer. It
has not been tested with other installers, like InstallShield.
Author: Rene Horn, [email protected]
Version: 0.21
Known issue: this currently only works using msiexec to uninstall things
- has not been tested with other install programs (e.g. InstallShield)
#>
<#
Copyright (c) 2015, Rene Horn
All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#>
param (
[parameter(Mandatory=$true)]
[alias("p","Publisher")]
[string]$SearchString,
[parameter(Mandatory=$false)]
[alias("s")]
[switch]$silent
)
if (!$silent) {
[System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") | Out-Null
}
function Get-ExitCode($process)
{ # different code paths necessary to get the exit code from the process depending on the version of PowerShell being used
if (4 -le $PSVersionTable.PSVersion.Major) {
return $process.ExitCode
} else {
# work around for bug https://connect.microsoft.com/PowerShell/feedback/details/520554/start-process-does-not-return-exitcode-property
$process.HasExited|Out-Null
return $process.GetType().GetField("exitCode","NonPublic,Instance").GetValue($process)
}
}
function Search-Installed($path, $SearchString)
{ # this is where we look for the search string in the list of installed applications
return @(Get-ChildItem -Recurse -ErrorAction SilentlyContinue -Path "${path}" | foreach { Get-ItemProperty -Path $_.PsPath }| Where-Object { ($_.DisplayName -like "*${SearchString}*" ) -or ($_.Publisher -like "*${SearchString}*") } | Select-Object UninstallString | % { if ( $_ -imatch "@\{UninstallString=[a-z.]+\s*`/[xi]\s*`{[^}]+`}`}" ) { ($_ -replace "@`{[^{]*(`{[^}]+}).*",'$1') } })
}
function Write-ToProgress($Activity, $Status, [switch]$Completed)
{
if(!$silent) {
if ($Completed) {
Write-Progress -Activity $Activity -Status $Status -Complete
} else {
Write-Progress -Activity $Activity -Status $Status
}
}
}
function Show-MessageBox($Message, $SearchString)
{
if(!$silent) {
[System.Windows.Forms.MessageBox]::Show($Message,"Uninstall of `"$SearchString`" ","Ok","Information","Button1")
}
}
# setup some local variables
$uninst_reg = "Microsoft\Windows\CurrentVersion\Uninstall"
$base_reg_entry = "hklm:\\SOFTWARE"
$redir_32bit_reg = "Wow6432Node"
Write-ToProgress -Activity "Uninstall of `"$SearchString`"" -Status "Searching through installed applications for `"$SearchString`""
$clsid_list = Search-Installed "${base_reg_entry}\${uninst_reg}" $SearchString
$is_64bit = ([IntPtr]::size -eq 8)
if ($is_64bit) {
$clsid_list += Search-Installed "${base_reg_entry}\${redir_32bit_reg}\${uninst_reg}" $SearchString
}
$msi_exec = "${env:SystemRoot}\System32\msiexec.exe"
#$msi_exec = "C:\temp\showarguments.exe"
$msi_uninst_flag = @("/norestart","/q","/x")
$msi_log_flag = "/l*v"
$msi_log = "${env:SystemDrive}\uninstall_${SearchString}.{0}.log"
$clsid_counter = 1
# iterate through the list of applications that match our search string, and uninstall them
foreach ( $clsid in $clsid_list ) {
$msi_log_current = ($msi_log -f $clsid_counter)
Write-ToProgress -Activity "Uninstall of `"$SearchString`"" -Status ("Uninstalling $clsid_counter of {0}, log file: $msi_log_current" -f @($clsid_list).Count)
$msi_args = @($clsid, $msi_log_flag, $msi_log_current)
$msi_proc = Start-Process -FilePath $msi_exec -ArgumentList @($msi_uninst_flag+$msi_args) -PassThru -Wait
$msi_exitcode = Get-ExitCode $msi_proc
if ($msi_exitcode -ne 0) {
Write-Error "Error uninstalling, msiexec.exe exit code ${msi_exitcode}, check ${msi_log_current}"
Exit $msi_exitcode
}
$clsid_counter++
}
Write-ToProgress -Activity "Uninstall of `"$SearchString`"" -Status "Completing uninstall." -Completed
Show-MessageBox -Message "Uninstall of `"$SearchString`" complete." -SearchString $SearchString
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment