How to Get Human-Readable Errors in PowerShell When an App Isn’t Installed?

0
0
Asked By CuriousCoder92 On

I'm working on a PowerShell script to provide clear error messages when a specific application isn't installed. The script is going to be run through Intune Scripts and Remediation, which only captures the last output of the PowerShell script. Here's what I've written so far:

```powershell
$Application = get-package "Application" -ErrorAction Stop | Where-Object { $_.metadata['installlocation'] }

if (!(Test-Path $Folder)) {
try {
Write-Output $Application
}
catch {
Write-Output "Application not installed"
}
}
```

The issue I'm running into is that it displays an error message that the package can't be found, along with some default error codes from PowerShell, but the catch error for "Application not installed" isn't visible. I've also tried using Write-Host and Write-Error in the catch block, but nothing displays. What am I doing wrong?

1 Answer

Answered By DevGuru88 On

It seems your command `$Application...` is causing the script to stop before reaching the try/catch. The `-ErrorAction Stop` option means if that line fails, it exits the entire script. If you want to use try/catch, either handle potential nulls by checking if `$Application` is `$null`, or put the `Get-Package` command directly inside the try block as I suggested earlier.

CuriousCoder92 -

I overlooked that! Thanks for pointing it out. I’ve sorted it now!

Related Questions

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.