This PowerShell script should uninstall all Windows 11/10 bloatware, except $WhitelistedApps
, When I run the script on a VM, it tries to uninstall all bloatware, even $WhitelistedApps
!
This is a waste of time, the script will try to uninstall WhitelistedApps
, but they are already NonRemovable, so I want to exclude them.
Please, Tell me: What's wrong with the code?, and how can it be corrected?
I suspicious of the way i used -NotLike
, or maybe the way of quotation marks ""
that have package names.
$WhitelistedApps = @(
"1527c705-839a-4832-9118-54d4Bd6a0c89"
"E2A4F912-2574-4A75-9BB0-0D023378592B"
"F46D4000-FD22-4DB4-AC8E-4E1DDDE828FE"
"Microsoft.AAD.BrokerPlugin"
"Microsoft.AccountsControl"
"Microsoft.AsyncTextService"
"Microsoft.BioEnrollment"
"Microsoft.CredDialogHost"
"Microsoft.ECApp"
"Microsoft.LockApp"
"Microsoft.MicrosoftEdge"
"Microsoft.MicrosoftEdgeDevToolsClient"
"Microsoft.PPIProjection"
"Microsoft.Win32WebViewHost"
"Microsoft.Windows.Apprep.ChxApp"
"Microsoft.Windows.AssignedAccessLockApp"
"Microsoft.Windows.CallingShellApp"
"Microsoft.Windows.CapturePicker"
"Microsoft.Windows.CloudExperienceHost"
"Microsoft.Windows.ContentDeliveryManager"
"Microsoft.Windows.Cortana"
"Microsoft.Windows.NarratorQuickStart"
"Microsoft.Windows.OOBENetworkCaptivePortal"
"Microsoft.Windows.OOBENetworkConnectionFlow"
"Microsoft.Windows.ParentalControls"
"Microsoft.Windows.PeopleExperienceHost"
"Microsoft.Windows.PinningConfirmationDialog"
"Microsoft.Windows.Search"
"Microsoft.Windows.SecHealthUI"
"Microsoft.Windows.SecondaryTileExperience"
"Microsoft.Windows.SecureAssessmentBrowser"
"Microsoft.Windows.ShellExperienceHost"
"Microsoft.Windows.StartMenuExperienceHost"
"Microsoft.Windows.XGpuEjectDialog"
"Microsoft.XboxGameCallableUI"
"MicrosoftWindows.Client.CBS"
"MicrosoftWindows.UndockedDevKit"
"NcsiUwpApp"
"Windows.CBSPreview"
"Windows.ContactSupport"
"Windows.MiracastView"
"Windows.PrintDialog"
"c5e2524a-ea46-4f67-841f-6a9465d9d515"
"windows.immersivecontrolpanel"
)
foreach ($App in $WhitelistedApps) {
Get-AppxPackage | Where-Object {$_.Name -NotLike $App} | Remove-AppxPackage
}
}
CodePudding user response:
My bad on my last comment, I didn't see the foreach
statement on your code hence why recommended to use the -notin
operator instead of -NotLike
.
The easiest approach in this case would be to remove the foreach
loop and just change the operator for -notin
as suggested:
Get-AppxPackage | Where-Object {$_.Name -notin $WhitelistedApps} | Remove-AppxPackage
To explain what is wrong with your code, first of all, you are running Get-AppxPackage
a total of 44 times (the number of elements on the $WhitelistedApps
array) and since you're looping over this array, the condition:
Where-Object {$_.Name -NotLike $App}
Will return those packages you don't want to remove, a total of 43 times. This can be tested by following this example:
- Run the loop without
Remove-AppxPackage
:
$apps = foreach($App in $WhitelistedApps)
{
Get-AppxPackage | Where-Object {$_.Name -NotLike $App}
}
- Filter the result
$apps
array by those elements found on$WhitelistedApps
and then group them byName
:
$apps.Where({$_.Name -in $WhitelistedApps}) | Group-Object Name
Result should be something like this:
Count Name Group
----- ---- -----
43 windows.immersivecontr... {windows.immersivecontrolpanel_10.0.2.1000_neutral_neutral_cw5n1h2txyewy, windows.immersivecontrolpanel_10.0.2.1000_neutral_neutr...
43 Windows.PrintDialog {Windows.PrintDialog_6.2.1.0_neutral_neutral_cw5n1h2txyewy, Windows.PrintDialog_6.2.1.0_neutral_neutral_cw5n1h2txyewy, Windows.Pr...
43 Microsoft.BioEnrollment {Microsoft.BioEnrollment_10.0.19041.1023_neutral__cw5n1h2txyewy, Microsoft.BioEnrollment_10.0.19041.1023_neutral__cw5n1h2txyewy, ...
43 Microsoft.Windows.OOBE... {Microsoft.Windows.OOBENetworkConnectionFlow_10.0.19041.1023_neutral__cw5n1h2txyewy, Microsoft.Windows.OOBENetworkConnectionFlow_...
43 Microsoft.AAD.BrokerPl... {Microsoft.AAD.BrokerPlugin_1000.19041.1023.0_neutral_neutral_cw5n1h2txyewy, Microsoft.AAD.BrokerPlugin_1000.19041.1023.0_neutral...
43 Microsoft.Windows.OOBE... {Microsoft.Windows.OOBENetworkCaptivePortal_10.0.19041.1023_neutral__cw5n1h2txyewy, Microsoft.Windows.OOBENetworkCaptivePortal_10.
...
...
If you wanted to use a foreach
loop to filter the Packages that don't exist on the $WhitelistedApps
array, the code should look like this (note that, instead of looping over $WhitelistedApps
, we're looping over the Packages just one time):
foreach($App in Get-AppxPackage)
{
if($App.Name -notin $WhitelistedApps)
{
$App
}
}