Answers:
$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'"
(gwmi Win32_Product | ? Name -eq "Software").uninstall()
一点代码高尔夫。
编辑:多年来,这个答案已经获得了很多好评。我想补充一些意见。从那以后我就没有使用过PowerShell,但是我记得观察到一些问题:
-First 1
但不确定。随时编辑。使用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}
-like "appNam*"
因为版本在名称中,并且会更改,但是似乎找不到该程序。有任何想法吗?
要解决Jeff Hillman帖子中的第二种方法,您可以执行以下操作:
$app = Get-WmiObject
-Query "SELECT * FROM Win32_Product WHERE Name = 'Software Name'"
要么
$app = Get-WmiObject -Class Win32_Product `
-Filter "Name = 'Software Name'"
我发现不推荐使用Win32_Product类,因为它会触发修复并且未对查询进行优化。资源
我从Sitaram Pamarthi 找到了这篇文章,其中包含一个脚本,如果您知道应用程序guid,则可以将其卸载。他还提供了另一个脚本来在此处真正快速地搜索应用程序。
像这样使用:。\ 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"}
}
为了给这篇文章增加一点,我需要能够从多个服务器上删除软件。我用杰夫的答案来引导我:
首先,我获得了服务器列表,使用了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属性而不是名称进行匹配,以确保我正在卸载正确的应用程序。
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"
一行代码:
get-package *notepad* |% { & $_.Meta.Attributes["UninstallString"]}
这是使用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"
基于杰夫·希尔曼的答案:
这是您可以添加到您的函数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
可能需要一些时间,请耐心等待!
用:
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输入的软件原因。
未经测试:
它没有:
我无法使用uninstall()。尝试收到一个错误消息,告诉我无法为值为NULL的表达式调用方法。取而代之的是,我使用了Remove-WmiObject,它似乎可以达到相同的效果。
注意:如果没有提供计算机名称,它将从Active Directory中的所有系统中删除该软件。
对于我的大多数程序,这篇文章中的脚本都可以完成工作。但是我不得不面对一个无法使用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可以工作