有没有一种简单的方法,使用PowerShell钩入标准的“添加或删除程序”功能来卸载现有应用程序?或者检查该应用程序是否已安装?
$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'"
编辑:这个答案多年来得到了很多赞。我想添加一些评论。 我自那以后就没有使用过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*"
,因为版本号在名称中并且会更改,但似乎无法找到该程序。有什么想法吗? - NSouth为了修复 Jeff Hillman 帖子中的第二种方法,您可以执行以下操作:
$app = Get-WmiObject
-Query "SELECT * FROM Win32_Product WHERE Name = 'Software Name'"
或者
$app = Get-WmiObject -Class Win32_Product `
-Filter "Name = 'Software Name'"
一行代码:
get-package *notepad* |% { & $_.Meta.Attributes["UninstallString"]}
CategoryInfo: InvalidOperation: (:) [], RuntimeException FullyQualifiedErrorId: BadExpression
。 - mguttfunction 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"
$computers = @("computer1", "computer2", "computer3")
foreach($server in $computers){
$app = Get-WmiObject -Class Win32_Product -computer $server | Where-Object {
$_.IdentifyingNumber -match "5A5F312145AE-0252130-432C34-9D89-1"
}
$app.Uninstall()
}
我使用IdentifyingNumber属性进行匹配,而不是名称,只是为了确保我正在卸载正确的应用程序。
[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"}
}
以下是使用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"
我会做出自己的微小贡献。我需要从同一台计算机中删除一系列软件包。这是我想出来的脚本。
$packages = @("package1", "package2", "package3")
foreach($package in $packages){
$app = Get-WmiObject -Class Win32_Product | Where-Object {
$_.Name -match "$package"
}
$app.Uninstall()
}
我希望这对你有所帮助。
请注意,由于这个脚本是基于David Stetler的脚本开发的,所以他应该得到相应的荣誉。
基于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
可能需要一些时间,请耐心等待!