Keyed Arrays in Powershell
I found this old PowerShell file from 2015 that removes useless Windows 10 apps. Very useful. But for code compression reasons I observed it being written like this, which I know is just a waste of repeating statements:
Write-Host -NoNewline "Removing Candy Crush App..." -ForegroundColor White
Get-AppxPackage -AllUsers | Where-Object {$_.Name -like "king.com*"} | $
Write-Host "DONE" -ForegroundColor Green
Write-Host -Nonewline "Removing Twitter App..." -ForegroundColor White
Get-AppxPackage -AllUsers | Where-Object {$_.Name -like "*Twitter"} | Remove-AppxPackage
Write-Host "DONE" -ForegroundColor Green
Write-Host -Nonewline "Removing Facebook App..." -ForegroundColor White
Get-AppxPackage -AllUsers | Where-Object {$_.Name -like "*Facebook"} | Remove-AppxPackage
Write-Host "DONE" -ForegroundColor Green
...
# News / Sports / Weather
If ($App.DisplayName -eq "Microsoft.BingFinance")
{
Write-Host -NoNewline "Removing Finance App..." -ForegroundColor Yellow
Remove-AppxProvisionedPackage -Online -PackageName $App.PackageName | Out-Null
Remove-AppxPackage -Package $App.PackageName | Out-Null
Write-Host "DONE" -ForegroundColor Green
}
If ($App.DisplayName -eq "Microsoft.BingNews")
{
Write-Host -NoNewline "Removing News App..." -ForegroundColor Yellow
Remove-AppxProvisionedPackage -Online -PackageName $App.PackageName | Out-Null
Remove-AppxPackage -Package $App.PackageName | Out-Null
Write-Host "DONE" -ForegroundColor Green
}
...
My idea: store each app and its displayed status message that you see in each Write-Host automatically as a keyed array, example like this:
$Apps (
[0] (
[string]$StatusMessage = "Removing Candy Crush App...",
[object]$AppObject = object
[string]$AppType = "allusers"
)
...
[7] (
[string]$StatusMessage = "Removing Bing News app..."
[object]$AppObject = object
[string]$AppType = "provisioned"
)
[8] (
[string]$StatusMessage = "Removing Bing Finance app..."
[object]$AppObject = object
[string]$AppType = "provisioned"
)
...
)
And our "blacklist" from configs, which I would like to match with $Apps[key].AppOject.Name:
$Blacklist (
"king.com",
"*Twitter",
"*Facebook",
"Microsoft.BingFinance",
"Microsoft.BingNews",
...
)
That way I can iteritevly deal with them in one simple For Each $Apps as $App, forking if it needs deleted as a provisioned app rather than a regular user app, and output the splendid process as a pretty custom ANSI bar for the user, because we have a keyed array to go off of with a count of apps that actually exist that will most definitely match our blacklist. :-)
How would I store an array of our apps in a keyed array like this, so I could easily just do some string matching in a For Each $Apps as $App to handle each one properly?`