前言:
众所周知,在命令行可以使用msiexec.exe卸载应用程序,但是比较麻烦的是需要获取ProductCode或者找到安装时的MSI程序包,而且同一个产品可能会有很多的版本,而每个版本的ProductCode又会不一样,所以这对同产品的批量卸载很不友好。那么有没有什么方法可以自动获取到ProductCode实现卸载操作呢?当然是有的,而且很多。
下面我介绍一种比较简单直观的方法给大家,希望对各位有所帮助。
交互式powershell卸载软件脚本代码如下:
echo "本地计算机上已安装的应用程序:"
get-wmiobject Win32_Product #列出本地计算机上已安装的进程
$app = Read-Host "请输入需要卸载的应用名称"
$app_List = Get-WmiObject -Class Win32_Product -Filter "Name='$app'"
echo "即将卸载的应用程序信息:"
$app_List
$app_ProductCode = $app_List.IdentifyingNumber #提取应用程序ProductCode值
#静默卸载:msiexec.exe /x $app_ProductCode /quiet /norestart
msiexec.exe /x $app_ProductCode /passive /norestart
pause
可以批量卸载的powershell脚本代码如下(无需用户干预):
注意:如果应用程序有自我保护的,需要取消自我保护例如密码等才能卸载的应用程序
$app = 直接填写需要卸载的应用程序名称
$app_List = Get-WmiObject -Class Win32_Product -Filter "Name='$app'"
echo "即将卸载的应用程序信息:"
$app_List
$app_ProductCode = $app_List.IdentifyingNumber #提取应用程序ProductCode值
msiexec.exe /x $app_ProductCode /quiet /norestart #静默卸载
给大家分享一下这个卸载的构建思路:
我们的核心是需要提取ProductCode赋值给msiexec.exe使用,所以我们首先应该提取ProductCode。
1、需要使用get-wmiobject Win32_Product获取已安装的应用程序列表,以sep卸载为例
2、使用-Filter 参数从已安装列表中过滤出需要卸载的sep应用程序信息,从图中可以看到sep的信息已经被过滤出来。该值将存储在变量$app_List
中 。
输出的信息中有IdentifyingNumber、Name、Vendor、Version 字段,其中IdentifyingNumber字段的值是我们需要的,也就是ProductCode
4、提取ProductCode,在变量$app_List中提取IdentifyingNumber类型
5、赋值ProductCode给msiexec.exe,执行卸载命令
注意:如果应用程序有自我保护的,例如卸载密码保护,则需要填入密码才能卸载应用程序,或者在后台统一取消卸载密码保护