Powershell 卸载程序

 •  Filed under windows, powershell
function Uninstall-Application {
    $appName = $($args[0])
    Write "application to uninstall: $appName"
    $uninstall32 = gci "HKLM:\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall" | foreach { gp $_.PSPath } | ? { $_ -Match $appName }
    $uninstall64 = gci "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall" | foreach { gp $_.PSPath } | ? { $_ -match $appName }

    if($uninstall32){
        if($uninstall32.QuietUninstallString){
            Write ("uninstall32 Quiet: " + $uninstall32.QuietUninstallString)
            Start-Process -FilePath cmd.exe -ArgumentList '/c', $uninstall32.QuietUninstallString  -Wait
        }else{
            Write ("uninstall32 Normal: " + $uninstall32.UninstallString)
            Start-Process -FilePath cmd.exe -ArgumentList '/c', $uninstall32.UninstallString  -Wait
        }
    }
    if($uninstall64){
        if($uninstall64.QuietUninstallString){
            Write ("uninstall64 Quiet: " + $uninstall64.QuietUninstallString)
            Start-Process -FilePath cmd.exe -ArgumentList '/c', $uninstall64.QuietUninstallString  -Wait
        }else{
            Write ("uninstall64 Normal: " + $uninstall64.UninstallString)
            Start-Process -FilePath cmd.exe -ArgumentList '/c', $uninstall64.UninstallString  -Wait
        }
    }
}

Uninstall-Application("application name*")