#***********************************************************************
# "Uninstall-Toolbars.ps1"
#
# Written by Aaron Wurthmann (aaron <AT> wurthmann <DOT> com)
#
# If you edit please keep my name as an original author and
# keep me apprised of the changes, see email address above.
# This code may not be used for commercial purposes.
# You the executor,runner,user accept all liability.
# This code comes with ABSOLUTELY NO WARRANTY.
# You may redistribute copies of the code under the terms of the GPL v2.
# -----------------------------------------------------------------------
# Prerequisite:
# Should work with both PowerShell v1 and v2. I wrote and tested it on XP
# as well was Windows 7 x64. Make sure to have your execution mode is set
# properly. PSH> get-help about_Execution_Policies
# In addition, WASP, Windows Automation Snapin for PowerShell, was used
# for the macros to run and click items as needed, it is not mandatory.
# -----------------------------------------------------------------------
# 2010.03.15 ver 1.2
#
# Summary:
# Removes software based on display name shown in "Add Remove Programs".
# In the majority of cases it just runs whatever uninstall program the
# program claims will uninstall itself. In a few cases a macro is run.
# See $apps array section and comments to add to the list of programs.
#
# Known Bugs:
# At this time I have noticed an issue with the "Bing Bar" attemping to
# re-install itself. Actually what is happening is a related install
# 'Bing Bar Platform' is being launched and as a result it asks if you
# want to reinstall. I haven't looked into solving this just yet. My advice
# at the moment is... don't reinstall it. *laugh*
#************************************************************************

#Function Section - Dont Touch, unless you Know what you are doing.
function Get-AppInfo {
    Param([string]$filter)
    $regkey=Get-Item "HKLM:SOFTWAREMicrosoftWindowsCurrentVersionUninstall"
    $SubKeyNames = $regkey.GetSubKeyNames()
    foreach($SubKeyName in $SubKeyNames){
        $SubKeyString="$regkey$SubKeyName" -replace "HKEY_LOCAL_MACHINE\","HKLM:"
        $SubKey=Get-Item $SubKeyString
        $DisplayName=$SubKey.GetValue('DisplayName')
        If ($DisplayName -like "*$filter*"){
            $DisplayName
            $SubKey.GetValue('UninstallString')
        }
    }
}

function Remove-MSI {
    Param([string]$uninstallString)
    Stop-Webbrowser
    $uninstall=$uninstallString.Replace('/I','/X')
    $uninstallCmd=$uninstall.split(' ')[0]
    $uninstallParam=$uninstall.split(' ')[1]
    & $uninstallCmd $uninstallParam /qn
}

function Remove-Install {
    Param([string]$uninstallString)
    Stop-Webbrowser
    $uninstallCmd=$UninstallString.split('"')[1]
    if ($uninstallCmd){
        $uninstallCmd=$uninstallCmd.Trim()
        $uninstallParam=$UninstallString.split('"')[2]
        if ($uninstallParam){
            $uninstallParam=$uninstallParam.Trim()
        }
    }
    ELSE{
        $uninstallCmd=$UninstallString.split('/')[0].Trim()
        $uninstallParam=$UninstallString.split('/')[1]
        if ($uninstallParam){
            $uninstallParam='/' +$uninstallParam.Trim()
        }
    }

    & $uninstallCmd $uninstallParam
}

function Stop-Webbrowser {
    Get-Process | Where {$_.Name -eq "iexplore"} | kill
    #Get-Process | Where {$_.Name -eq "firefox"} | kill
}

function Remove-AltavistaToolbar {
    Param([string]$uninstallString)
    Stop-Webbrowser
    & $UninstallString.split('/')[0]
    Add-PSSnapin WASP -ea "SilentlyContinue"
    Select-Window > $nul
    if ($? -eq $True){
        Start-Sleep 1
        Select-Window uninstall | Send-Keys "y"
        Start-Sleep 1
        Stop-Webbrowser
    }
    ELSE{
        Remove-Install $UninstallString
    }
}

function Remove-BingBar {
    Param([string]$uninstallString)
    Stop-Webbrowser
    & $UninstallString.split('/')[0]
    Add-PSSnapin WASP -ea "SilentlyContinue"
    Select-Window > $nul
    if ($? -eq $True){
        Start-Sleep 1
        Select-Window InstallManager | Send-Keys "{TAB}","{ENTER}"
        Stop-Webbrowser
    }
    ELSE{
        Remove-Install $UninstallString
    }
}

function Remove-Yahoo!Toolbar {
    Param([string]$uninstallString)
    Stop-Webbrowser
    & $UninstallString.split('/')[0]
    Add-PSSnapin WASP -ea "SilentlyContinue"
    Select-Window > $nul
    if ($? -eq $True){
        Start-Sleep 1
        Select-Window UNYT_W~1 | Send-Keys "y"
        Start-Sleep 2
        Select-Window BU_ | Select-ChildWindow | Send-Keys "n"
        Start-Sleep 1
        Select-Window BU_ | Send-Keys "c"
        Start-Sleep 1
        Stop-Webbrowser
    }
    ELSE{
        Remove-Install $UninstallString
    }
}
#End Fuction Section

#    Add Apps here that you want to remove. Use the exact name as it appears in "Add Remove Programs"
#    For all toolbars uncomment 'Toolbar' and remove all other toolbars except 'Bing Bar'
$apps=@(
    'Ask Toolbar',
    'Altavista Toolbar',
    'Bing Bar',
    'Google Toolbar for Internet Explorer',
    'Yahoo! Toolbar';
    #'Toolbar';
)

#These apps have extra routines, so there are custom functions.
#WASP, Windows Automation Snapin for PowerShell, is required for macros to run
$macroapps=@(
    'Altavista Toolbar',
    'Bing Bar',
    'Yahoo! Toolbar';
)
#End Editable Section

#Main
if ($apps){
    foreach($app in $apps){
        $AppInfo=(Get-AppInfo $app)
        $DisplayName=$AppInfo[0]
        $UninstallString=$AppInfo[1]
        if ($UninstallString -like "*MsiExec*"){
            Remove-MSI $UninstallString
        }
        Else{
            if ($macroapps -contains $DisplayName){
                $cutomRemove='Remove-' + $DisplayName.Replace(' ','')
                & $cutomRemove
            }
            ELSE{
                Remove-Install $UninstallString
            }
        }
    }
}
Be Sociable, Share!