如何使用PowerShell卸载应用程序?

问题描述 投票:116回答:12

有没有一种简单的方法可以使用PowerShell挂钩标准的“添加或删除程序”功能来卸载现有的应用程序?或者检查是否安装了应用程序?

windows powershell windows-installer uninstall
12个回答
146
投票
$app = Get-WmiObject -Class Win32_Product | Where-Object { 
    $_.Name -match "Software Name" 
}

$app.Uninstall()

编辑:Rob发现了另一种使用Filter参数的方法:

$app = Get-WmiObject -Class Win32_Product `
                     -Filter "Name = 'Software Name'"

1
投票

基于Jeff Hillman的回答:

这是一个可以添加到profile.ps1或在当前PowerShell会话中定义的函数:

# Uninstall a Windows program
function uninstall($programName)
{
    $app = Get-WmiObject -Class Win32_Product -Filter ("Name = '" + $programName + "'")
    if($app -ne $null)
    {
        $app.Uninstall()
    }
    else {
        echo ("Could not find program '" + $programName + "'")
    }
}

假设你想要卸载Notepad++。只需在PowerShell中键入:

> uninstall("notepad++")

请注意Get-WmiObject可能需要一些时间,所以请耐心等待!


0
投票

使用:

function remove-HSsoftware{
[cmdletbinding()]
param(
[parameter(Mandatory=$true,
ValuefromPipeline = $true,
HelpMessage="IdentifyingNumber can be retrieved with `"get-wmiobject -class win32_product`"")]
[ValidatePattern('{[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{12}}')]
[string[]]$ids,
[parameter(Mandatory=$false,
            ValuefromPipeline=$true,
            ValueFromPipelineByPropertyName=$true,
            HelpMessage="Computer name or IP adress to query via WMI")]
[Alias('hostname,CN,computername')]
[string[]]$computers
)
begin {}
process{
    if($computers -eq $null){
    $computers = Get-ADComputer -Filter * | Select dnshostname |%{$_.dnshostname}
    }
    foreach($computer in $computers){
        foreach($id in $ids){
            write-host "Trying to uninstall sofware with ID ", "$id", "from computer ", "$computer"
            $app = Get-WmiObject -class Win32_Product -Computername "$computer" -Filter "IdentifyingNumber = '$id'"
            $app | Remove-WmiObject

        }
    }
}
end{}}
 remove-hssoftware -ids "{8C299CF3-E529-414E-AKD8-68C23BA4CBE8}","{5A9C53A5-FF48-497D-AB86-1F6418B569B9}","{62092246-CFA2-4452-BEDB-62AC4BCE6C26}"

它没有经过全面测试,但它在PowerShell 4下运行。

我在这里看到的是运行PS1文件。让它从AD检索所有系统并尝试在所有系统上卸载多个应用程序。

我已经使用IdentifyingNumber来搜索David Stetlers输入的软件原因。

未经测试:

  1. 不将id添加到脚本中的函数调用中,而是使用参数ID启动脚本
  2. 使用多于1个计算机名称调用脚本不会自动从函数中检索
  3. 从管道中检索数据
  4. 使用IP地址连接到系统

它没有:

  1. 如果在任何给定系统上实际找到软件,它不会提供任何信息。
  2. 它没有提供有关卸载失败或成功的任何信息。

我无法使用uninstall()。尝试我得到一个错误,告诉我调用一个值为NULL的表达式的方法是不可能的。相反,我使用了Remove-WmiObject,它似乎也是如此。

注意:如果没有给出计算机名称,它将从Active Directory中的所有系统中删除该软件。


0
投票

对于我的大多数程序,本文中的脚本都可以完成这项任务。但我不得不面对一个遗留的程序,我无法使用msiexec.exe或Win32_Product类删除。 (从某种原因我得到了0号出口,但程序还在那里)

我的解决方案是使用Win32_Process类:

nickdnk的帮助下,这个命令是获取卸载exe文件的路径:

64位:

[array]$unInstallPathReg= gci "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall" | foreach { gp $_.PSPath } | ? { $_ -match $programName } | select UninstallString

32位:

 [array]$unInstallPathReg= gci "HKLM:\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall" | foreach { gp $_.PSPath } | ? { $_ -match $programName } | select UninstallString

你将不得不清理结果字符串:

$uninstallPath = $unInstallPathReg[0].UninstallString
$uninstallPath = $uninstallPath -Replace "msiexec.exe","" -Replace "/I","" -Replace "/X",""
$uninstallPath = $uninstallPath .Trim()

现在当你有相关的程序卸载exe文件路径时,你可以使用这个命令:

$uninstallResult = (Get-WMIObject -List -Verbose | Where-Object {$_.Name -eq "Win32_Process"}).InvokeMethod("Create","$unInstallPath")

$ uninstallResult - 将有退出代码。 0是成功的

上面的命令也可以远程运行 - 我使用invoke命令,但我相信添加参数-computername可以工作


42
投票

编辑:多年来,这个答案得到了很多赞成。我想补充一些意见。我从未使用PowerShell,但我记得观察过一些问题:

  1. 如果以下脚本的匹配数多于1,则它不起作用,您必须附加将结果限制为1的PowerShell过滤器。我相信它是-First 1但我不确定。随意编辑。
  2. 如果MSI没有安装该应用程序,则它不起作用。它编写如下的原因是因为它修改MSI以在没有干预的情况下卸载,这在使用本机卸载字符串时并不总是默认情况。

使用WMI对象需要永远。如果您只知道要卸载的程序的名称,则速度非常快。

$uninstall32 = gci "HKLM:\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall" | foreach { gp $_.PSPath } | ? { $_ -match "SOFTWARE NAME" } | select UninstallString
$uninstall64 = gci "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall" | foreach { gp $_.PSPath } | ? { $_ -match "SOFTWARE NAME" } | select UninstallString

if ($uninstall64) {
$uninstall64 = $uninstall64.UninstallString -Replace "msiexec.exe","" -Replace "/I","" -Replace "/X",""
$uninstall64 = $uninstall64.Trim()
Write "Uninstalling..."
start-process "msiexec.exe" -arg "/X $uninstall64 /qb" -Wait}
if ($uninstall32) {
$uninstall32 = $uninstall32.UninstallString -Replace "msiexec.exe","" -Replace "/I","" -Replace "/X",""
$uninstall32 = $uninstall32.Trim()
Write "Uninstalling..."
start-process "msiexec.exe" -arg "/X $uninstall32 /qb" -Wait}

34
投票

要修复Jeff Hillman的帖子中的第二种方法,你可以做一个:

$app = Get-WmiObject 
            -Query "SELECT * FROM Win32_Product WHERE Name = 'Software Name'"

要么

$app = Get-WmiObject -Class Win32_Product `
                     -Filter "Name = 'Software Name'"

7
投票

要在这篇文章中添加一些内容,我需要能够从多个服务器中删除软件。我用杰夫的回答引导我:

首先我得到了一个服务器列表,我使用了AD查询,但你可以提供你想要的计算机名称数组:

$computers = @("computer1", "computer2", "computer3")

然后我循环遍历它们,将-computer参数添加到gwmi查询:

foreach($server in $computers){
    $app = Get-WmiObject -Class Win32_Product -computer $server | Where-Object {
        $_.IdentifyingNumber -match "5A5F312145AE-0252130-432C34-9D89-1"
    }
    $app.Uninstall()
}

我使用IdentifyingNumber属性来匹配而不是名称,只是为了确保我正在卸载正确的应用程序。


6
投票

我发现不建议使用Win32_Product类,因为它会触发修复并且不会进行查询优化。 Source

我发现来自Sitaram Pamarthi的this post有一个脚本要卸载,如果你知道应用程序guid。他还提供了另一个脚本来快速搜索here的应用程序。

使用如下:。\ uninstall.ps1 -GUID {C9E7751E-88ED-36CF-B610-71A1D262E906}

[cmdletbinding()]            

param (            

 [parameter(ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true)]
 [string]$ComputerName = $env:computername,
 [parameter(ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true,Mandatory=$true)]
 [string]$AppGUID
)            

 try {
  $returnval = ([WMICLASS]"\\$computerName\ROOT\CIMV2:win32_process").Create("msiexec `/x$AppGUID `/norestart `/qn")
 } catch {
  write-error "Failed to trigger the uninstallation. Review the error message"
  $_
  exit
 }
 switch ($($returnval.returnvalue)){
  0 { "Uninstallation command triggered successfully" }
  2 { "You don't have sufficient permissions to trigger the command on $Computer" }
  3 { "You don't have sufficient permissions to trigger the command on $Computer" }
  8 { "An unknown error has occurred" }
  9 { "Path Not Found" }
  9 { "Invalid Parameter"}
 }

5
投票
function Uninstall-App {
    Write-Output "Uninstalling $($args[0])"
    foreach($obj in Get-ChildItem "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall") {
        $dname = $obj.GetValue("DisplayName")
        if ($dname -contains $args[0]) {
            $uninstString = $obj.GetValue("UninstallString")
            foreach ($line in $uninstString) {
                $found = $line -match '(\{.+\}).*'
                If ($found) {
                    $appid = $matches[1]
                    Write-Output $appid
                    start-process "msiexec.exe" -arg "/X $appid /qb" -Wait
                }
            }
        }
    }
}

这样称呼它:

Uninstall-App "Autodesk Revit DB Link 2019"

3
投票

我会做出自己的小小贡献。我需要从同一台计算机中删除一个包列表。这是我提出的脚本。

$packages = @("package1", "package2", "package3")
foreach($package in $packages){
  $app = Get-WmiObject -Class Win32_Product | Where-Object {
    $_.Name -match "$package"
  }
  $app.Uninstall()
}

我希望这证明是有用的。

请注意,我欠David Stetler这个剧本的功劳,因为它基于他的。


2
投票

以下是使用msiexec的PowerShell脚本:

echo "Getting product code"
$ProductCode = Get-WmiObject win32_product -Filter "Name='Name of my Software in Add Remove Program Window'" | Select-Object -Expand IdentifyingNumber
echo "removing Product"
# Out-Null argument is just for keeping the power shell command window waiting for msiexec command to finish else it moves to execute the next echo command
& msiexec /x $ProductCode | Out-Null
echo "uninstallation finished"

2
投票

一行代码:

get-package *notepad* |% { & $_.Meta.Attributes["UninstallString"]}
© www.soinside.com 2019 - 2024. All rights reserved.