Q:

How to avoid error when Get-AppxPackageManifest not found

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.

A:

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)

