如何使用CNotLike制作数组

时间:2019-02-22 08:39:25

标签: powershell

$intel = "AppUp.IntelGraphicsControlPanel"
$nvidia = "NVIDIACorp.NVIDIAControlPanel"
$store = "*Store*"
Get-AppxProvisionedPackage -Online | Where-Object {$_.DisplayName -CNotLike $intel -and $_.DisplayName -CNotLike $nvidia -and $_.DisplayName -CNotLike $store} | Remove-AppxProvisionedPackage -Online -ErrorAction SilentlyContinue

由于带变量的字符串太长,是否可以使用数组? (可能是)

$apps = @()
Foreach ($app in $apps)
{    }

2 个答案:

答案 0 :(得分:1)

我认为您可以使用正则表达式-cnotlike来缩短重复的-cnotmath,如下所示:

$re = "AppUp\.IntelGraphicsControlPanel|NVIDIACorp\.NVIDIAControlPanel|.*Store.*"
Get-AppxProvisionedPackage -Online | 
    Where-Object {$_.DisplayName -cnotmatch $re} | 
        Remove-AppxProvisionedPackage -Online -ErrorAction SilentlyContinue

正则表达式详细信息:

                                 Match this alternative (attempting the next alternative only if this one fails)
   AppUp                         Match the character string “AppUp” literally (case sensitive)
   \.                            Match the character “.” literally
   IntelGraphicsControlPanel     Match the character string “IntelGraphicsControlPanel” literally (case sensitive)
|
                                 Or match this alternative (attempting the next alternative only if this one fails)
   NVIDIACorp                    Match the character string “NVIDIACorp” literally (case sensitive)
   \.                            Match the character “.” literally
   NVIDIAControlPanel            Match the character string “NVIDIAControlPanel” literally (case sensitive)
|
                                 Or match this alternative (the entire match attempt fails if this one fails to match)
   .                             Match any single character that is NOT a line break character (line feed)
      *                          Between zero and unlimited times, as many times as possible, giving back as needed (greedy)
   Store                         Match the character string “Store” literally (case sensitive)
   .                             Match any single character that is NOT a line break character (line feed)
      *                          Between zero and unlimited times, as many times as possible, giving back as needed (greedy)

答案 1 :(得分:0)

$apps = @(  
"AppUp.IntelGraphicsControlPanel",  
"Microsoft.LanguageExperiencePackru-RU",  
"Microsoft.Windows.Photos",  
"Microsoft.ScreenSketch",  
"NVIDIACorp.NVIDIAControlPanel",  
"Store")  
(Get-AppxPackage -AllUsers | Where-Object {$_.Name -notmatch ($apps -join '|')}).name