Irenicus
Irenicus

Reputation: 23

Comparing 2 Arrays and keeping columns that are similar

I am working on a way to remove default apps with a startup script on Windows 10 PCs.

Here is my script so far:

$RemovalList = @(
    'Microsoft.Messaging'
    'Microsoft.MicrosoftOfficeHub'
    'Microsoft.MicrosoftSolitaireCollection'
    'Microsoft.Office.OneNote'
    'Microsoft.SkypeApp'
    'microsoft.windowscommunicationsapps'
    'Microsoft.XboxApp'
    'Microsoft.XboxGameOverlay'
    'Microsoft.XboxIdentityProvider'
    'Microsoft.XboxSpeechToTextOverlay'
    'Microsoft.ZuneMusic'
    'Microsoft.ZuneVideo'
)

$RemovalList1 = "" | Select DisplayName
$RemovalList1.DisplayName = $RemovalList

$array = @();
$dismRaw = (DISM /Online /Get-ProvisionedAppxPackages)
$PNList =  ($dismRaw | Select-string PackageName) -replace "PackageName : ", ""
$DNList = ($dismRaw | Select-string DisplayName) -replace "DisplayName : ", ""
$Apps = "" | select DisplayName,PackageName
$Apps.DisplayName = $DNList
$Apps.PackageName = $PNList
$array += $Apps

$Apps = $Apps | Where {$_.DisplayName -eq $RemovalList1.DisplayName} #<-- Trying to remove the differences
$Apps.PackageName | % {dism /Online /Remove-ProvisionedAppxPackage /PackageName:$_}

What am I missing?

I want the $Apps array to remove anything, in the $RemovalList. Then I want to pass that to the command dism /Online /Remove-ProvisionedAppxPackage /PackageName:$_.

Update 5/17/2018 More Info: If I use The Remove-ProvisionedAppxPackage it removes for System and when the user logs into the system for the first time the apps will not install but the cmdlet will not work as a startup. It will not detect they are there. Using the "dism /Online /Remove-ProvisionedAppxPackage /PackageName:$_" works..... I wanted to take 2 arrays. First is the $RemovalList the second is the $apps. $apps has 2 columns DisplayName and PackageName. I want to remove the rows from $apps array that are not in the $RemovalList.

Another Update 5/17/2018 So instead of comparing the 2 arrays and striping out what is not in both, I decided to compare the PackageName with the $Removal List. It's working but still did not get my original question answered. :( Oh well it works.

New Code:

$RemovalList=@(
'Microsoft.Messaging'
'Microsoft.MicrosoftOfficeHub'
'Microsoft.MicrosoftSolitaireCollection'
'Microsoft.Office.OneNote'
'Microsoft.SkypeApp'
'microsoft.windowscommunicationsapps'
'Microsoft.XboxApp'
'Microsoft.XboxGameOverlay'
'Microsoft.XboxIdentityProvider'
'Microsoft.XboxSpeechToTextOverlay'
'Microsoft.ZuneMusic'
'Microsoft.ZuneVideo'
)

$dismRaw = (DISM /Online /Get-ProvisionedAppxPackages)
$PNList =  ($dismRaw | Select-string PackageName) -replace "PackageName : ", ""
$AppsToRemove = "" | select PackageName
$AppsToRemove.PackageName = $PNList | where {$RemovalList -contains $(($_).Split("_")[0])}

ForEach ($RemoveApp in $AppsToRemove.PackageName) {
    Write-Output "Removing: $RemoveApp"
    DISM /Online /Remove-ProvisionedAppxPackage /PackageName:$RemoveApp

}  

Thanks for your help! :)

Upvotes: 1

Views: 131

Answers (1)

Kory Gill
Kory Gill

Reputation: 7163

PowerShell has cmdlets for AppX stuff...your script can be written as:

$RemovalList=@(
'Microsoft.Messaging'
'Microsoft.MicrosoftOfficeHub'
'Microsoft.MicrosoftSolitaireCollection'
'Microsoft.Office.OneNote'
'Microsoft.SkypeApp'
'microsoft.windowscommunicationsapps'
'Microsoft.XboxApp'
'Microsoft.XboxGameOverlay'
'Microsoft.XboxIdentityProvider'
'Microsoft.XboxSpeechToTextOverlay'
'Microsoft.ZuneMusic'
'Microsoft.ZuneVideo'
)

$RemovalList | % {Get-AppxPackage -Name $_} | Remove-AppxPackage -WhatIf

Remove the -WhatIf to actually remove these things.

Also see this nice GitHub link for more stuff to control Windows items that you may not want.

Upvotes: 3

Related Questions