how to handle exception in powershell
This is because your Trap did not really handle the exception. To handle an exception, you need to add the Continue statement to your trap: Now, the trap works as expected. It does whatever you specified in the trap script block, and PowerShell does not get to see the exception anymore.You no longer get the red error message.ote that your Trap did not handle the exception because it did not call Continue. Instead, it just modified the existing error record and left it to PowerShell to output the error information. Of course, in real life you would first check error details and then create a meaningful replacement message.