1

我是 PowerShell 的新手,我正在寻找一种卸载多个应用程序的方法。我在一个文本文件中有一个要卸载的应用程序列表。这是我到目前为止的代码:

# Retrieve names of all softwares to un-install and places in variable $app

$App = Get-Content "C:\temp\un-installApps.txt"

# Cycle through each of the softwares to un-install and store in the WMI variable

Foreach ($AppName in $App)
{
    $AppTmp = Get-WmiObject -query "Select * from win32_product WHERE Name like" + $AppName 
    $AppNames = $AppNames + $AppTmp
}

foreach ($Application in $AppNames )
{
    msiexec /uninstall $Application.IdentifyingNumber
}

以下几行导致问题

$AppTmp = Get-WmiObject -query "Select * from win32_product WHERE Name like" + $AppName 
$AppNames = $AppNames + $AppTmp"

有什么想法可以让这个工作吗?

4

1 回答 1

3

我认为这是因为和应用程序名称之间没有空格,like并且应用程序名称周围需要有单引号。那部分应该是这样的like '" + $AppName + "'"

但是,您可以像这样更简单地执行整个脚本:

$App = Get-Content "C:\temp\un-installApps.txt"

gwmi win32_product|
    where { $App -contains $_.Name }|
    foreach { $_.Uninstall() } | out-null
于 2011-10-29T11:59:36.863 回答