0

This script lists the Windows Apps (Universal applications) installed on the system. It works but the command Get-AppxPackageManifest $app produces an error when the manifest file is not found. In my case, it happens for the app Windows.MiracastView.

I'm not familiar enough with PS scripting to write code to trap and avoid this error. What would be the "IF" command to skip an $app if its manifest file is not found?

$installedapps = get-AppxPackage
foreach ($app in $installedapps)
{
    echo $app.Name
    foreach ($id in (Get-AppxPackageManifest $app).package.applications.application.id)
    {
        $line = $app.Name + "=" + $app.packagefamilyname + "!" + $id
        echo $line
        $line >> 'C:\Temp\CollectWindowsAppsList.txt'
    }
}
echo "Press any key to continue..."
[void][System.Console]::ReadKey($true)

Thanks.

JnLlnd
  • 165
  • 1
  • 14
  • I commend to your attention [Microsoft Docs on PowerShell Common Parameters](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_commonparameters?view=powershell-6), specifically, look at `ErrorAction` and `ErrorVariable`. You should also look at [Microsoft Docs on `try/catch/finally`](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_try_catch_finally?view=powershell-6) for another (preferred) method of handling PowerShell errors. – Jeff Zeitlin Jun 14 '18 at 14:58
  • Hi Jeff. Thanks for these references. One of these days, I'll learn PS scripting language. But, for now I'm rushing to finish a project that need the output of this script and I would prefer to avoid the "red" error message. If someone could make the changes required in the code, I would greatly appreciate. – JnLlnd Jun 14 '18 at 15:34

1 Answers1

0

Try Catch block is one way to do this:

$installedapps = Get-AppxPackage

foreach ($app in $installedapps){

    Write-Output $app.Name 

    $ids = $null

    try{
        # Add ids to array
        $ids += (Get-AppxPackageManifest $app -erroraction Stop).package.applications.application.id
    }
    catch{
        # this catches everything so good idea to provide some feedback
        Write-Warning "No Id's found for $($app.name)" 
    }

    foreach ($id in $ids){
        $line = $app.Name + "=" + $app.packagefamilyname + "!" + $id
        Write-Output $line
        $line >> 'C:\Temp\CollectWindowsAppsList.txt'
    }
}

Write-Output "Press any key to continue..."
[void][System.Console]::ReadKey($true)
committhisgit
  • 110
  • 1
  • 6