编写脚本以摆脱某些Windows 10的过时软件并出现一些问题。
目的是要检查该应用程序是否存在,然后再使用Remove-AppXPackage将其删除。我将所有名称添加到字符串数组中,在字符串的两边都带有星号,但在if语句中使用-like或-match时找不到匹配项。
我编写了以下代码,以尝试使其完全按照以下命令运行:
Get-AppxPackage *windowsalarms* | Remove-AppxPackage
但是,首先检查该软件包是否存在。我假设原始命令将对象彼此匹配。而且我意识到我正在尝试将一个对象与字符串进行比较...那么我应该为每个对象创建一个仅具有name属性的对象数组,然后尝试对其进行比较吗?
$AppsToRemove = @("*broker*"
,"3dbuilder"
,"windowsalarms"
,"officehub"
,"skypeapp"
,"getstarted"
,"zunemusic"
,"solitarecollection"
,"bingfinance"
,"zunevideo"
,"bingnews"
,"windowsphone"
,"bingsports"
,"xboxapp"
,"whiteboard"
,"sway"
,"todos"
,"lens"
,"soundrecorder"
,"onenote"
,"remotedesktop"
,"networkspeedtest")
Get-AppxPackage | ForEach-Object {
$Placeholder = $_.Name
Foreach ($app in $AppsToRemove){
If ($app -like $Placeholder) {
Write-Host "Match"
}else{
Write-Host "No match"
}
}
}
答案 0 :(得分:2)
尝试类似的操作,将数组连接到[Regex]对象中。然后,我获得了所有AppXPackages并将其通过管道传递到Where-Object,以判断它们是否与原始数组匹配。我在末尾添加了-WhatIf
进行测试。当您要实际运行脚本来删除它们时,可以将其删除。
[Regex] $AppsToRemove = @(
'broker'
'3dbuilder'
'windowsalarms'
'officehub'
'skypeapp'
'getstarted'
'zunemusic'
'solitarecollection'
'bingfinance'
'zunevideo'
'bingnews'
'windowsphone'
'bingsports'
'xboxapp'
'whiteboard'
'sway'
'todos'
'lens'
'soundrecorder'
'onenote'
'remotedesktop'
'networkspeedtest'
) -join '|'
Get-AppxPackage | Where-Object -Property Name -Match -Value $AppsToRemove | Remove-AppxPackage -Verbose -WhatIf
鉴于其中一些已在我的计算机上删除,我得到以下输出:
What if: Performing the operation "Remove package" on target "Microsoft.AAD.BrokerPlugin_1000.16299.15.0_neutral_neutral_cw5n1h2txyewy".
What if: Performing the operation "Remove package" on target "Microsoft.WindowsAlarms_10.1804.1101.1000_x64__8wekyb3d8bbwe".
What if: Performing the operation "Remove package" on target "Microsoft.WindowsSoundRecorder_10.1804.911.1000_x64__8wekyb3d8bbwe".
What if: Performing the operation "Remove package" on target "Microsoft.ZuneVideo_10.18082.10311.0_x64__8wekyb3d8bbwe".
What if: Performing the operation "Remove package" on target "Microsoft.ZuneMusic_10.18091.10321.0_x64__8wekyb3d8bbwe".
答案 1 :(得分:-1)
@ShawnEsterman的回答将我引向了我更熟悉的想法,并提出了以下可行的方法:
$i = 0
$AppsToRemove = @("broker"
,"3dbuilder"
,"windowsalarms"
,"officehub"
,"skypeapp"
,"getstarted"
,"zunemusic"
,"solitarecollection"
,"bingfinance"
,"zunevideo"
,"bingnews"
,"windowsphone"
,"bingsports"
,"xboxapp"
,"whiteboard"
,"sway"
,"todos"
,"lens"
,"soundrecorder"
,"onenote"
,"remotedesktop"
,"networkspeedtest")
$Packages = Get-AppxPackage | ForEach-Object {
Foreach ($App in $AppsToRemove){
if($_ -match $App){
Remove-AppxPackage $_
}
}
}
感谢肖恩的指导!