How do I keep terminating errors out of the transcript and $error?

THEOBALD K 60 Reputation points
2025-12-18T23:56:32.3166667+00:00

I have a PS 5.1 script where I have some data in a file, and there are two ways to get the data. I want to try the first way, and try the second way if the first way fails. So my code looks something like this:

try { way1 } catch { way2 }

I include -ErrorAction Stop in the code for way 1 to make sure that non-terminating errors go to the catch block. But if I do this:

Start-Transcript -Path C:\temp\transtest.log

try {Import-Clixml -path $fn -erroraction stop} catch {'something else'}

Stop-Transcript

then if $fn is a valid file path that is NOT a valid CLIXML file, then the following is part of the log:

TerminatingError(Import-Clixml): "Data at the root level is invalid. Line 1, position 1."

I consider a failure of way 1 to be common behavior. As long as way 2 succeeds, it's not an error; it's EXPECTED, and I don't want that message cluttering the log file or the $error variable. The whole point of having a try-catch block is to contain errors and handle them there. So how do I keep the reporting of the error from "leaking" out of the block?

Windows for business | Windows Server | User experience | PowerShell
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Q&A Assist
    2025-12-18T23:56:42.8433333+00:00
    AI generated content. This question contains an answer created with AI from Q&A Assist. Learn more

    To prevent terminating errors from being logged in your transcript and $error variable when using PowerShell, you can use the -ErrorAction Stop parameter combined with a try/catch block. However, since you want to suppress the error messages that are considered expected behavior, you can utilize the -ErrorAction SilentlyContinue in your catch block or handle the error more gracefully.

    Here’s a modified version of your script:

    Start-Transcript -Path C:\temp\transtest.log
    
    try {
        Import-Clixml -Path $fn -ErrorAction Stop
    } catch {
        # Handle the expected error without cluttering the log
        if ($_.Exception -is [System.Management.Automation.TerminatingError]) {
            # Optionally log or handle the error silently
            # You can log a custom message if needed
            Write-Output "Handled expected error: $($_.Exception.Message)"
        } else {
            # If it's not the expected error, rethrow it
            throw
        }
    }
    
    Stop-Transcript
    

    In this script:

    • The Import-Clixml command is wrapped in a try block with -ErrorAction Stop to catch any terminating errors.
    • In the catch block, you can check if the caught exception is of a specific type (like TerminatingError) and handle it accordingly. This way, you can prevent it from being logged in the transcript or $error variable if it’s expected behavior.
    • If the error is not expected, you can rethrow it to ensure it’s logged appropriately.

    This approach allows you to manage expected errors without cluttering your logs while still handling unexpected errors properly.


    References:

    0 comments No comments

  2. Harry Phan 10,850 Reputation points Independent Advisor
    2025-12-19T01:07:20.8866667+00:00

    Hello THEOBALD K

    In PowerShell, Start-Transcript records all output streams, including error messages, even if they are caught in a try/catch block. To prevent the error from appearing in the transcript or $error variable, you can redirect the error stream for the first attempt (e.g., Import-Clixml -Path $fn -ErrorAction Stop 2>$null) so that the terminating error is handled only in your catch block. Another option is to use $Error.Clear() inside the catch block to ensure the error list remains clean after handling. This way, your “expected failure” in way 1 won’t clutter the logs, while way 2 can proceed normally.

    Please let me know if this resolves your scenario, and if you find the answer helpful, kindly hit “Accept Answer” 🙂. It helps us keep track of solutions and ensures others benefit from the guidance too.

    Harry.

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.