Hi there,
There are many ways to handle errors in Windows PowerShell, including:
$Error.Clear(); Do-Something; If ($Error) {..} Else {..}
Trap
$ErrorActionPreference
Trap blocks generally catch any errors in the scope of the entire script or function. The beauty of Try, Catch, Finally is that it is like a localized Trap for a specific block of commands. This gives you great flexibility in your error handling.
Here are some links to help you out
Using Try, Catch, Finally Blocks for PowerShell Error Handling
https://devblogs.microsoft.com/scripting/weekend-scripter-using-try-catch-finally-blocks-for-powershell-error-handling/
Handling Errors the PowerShell Way
https://devblogs.microsoft.com/scripting/handling-errors-the-powershell-way/
--If the reply is helpful, please Upvote and Accept it as an answer--