0

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?`

1 Answer 1

6

What it sounds like you want is a hashtable. With a hashtable you can define a key, and an associated value. For your purposes I would create a PSCustomObject for each value. Something like this:

$Apps = Get-AppxPackage -AllUsers
$Apps += Get-AppxProvisionedPackage -Online
$AppHash = @{
    'king.com' =          [pscustomobject]@{
                              'StatusMessage' = "Removing Candy Crush App..."
                              'AppObject' = $Apps | Where{$_.Name -like 'king.com*'}
                              'AppType' = 'AllUsers'
                          }
    'Microsoft.BingNews' = [pscustomobject]@{
                              'StatusMessage' = "Removing Candy Crush App..."
                              'AppObject' = $Apps | Where{$_.Name -like 'king.com*'}
                              'AppType' = 'Provisioned'
                          }
}

Then you just call it like you described:

Write-Host $AppHash['king.com'].StatusMessage
$AppHash['king.com'].AppObject | Remove-AppxPackage

You'll need to write a little more logic in there for handling provisioned things, but this should suit your needs for what you described.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you I'll give this a shot!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.