Logging system.io.pathtoolongexception

I’m new to PowerShell and I’m probably overlooking something obvious. I’m attempting to log system.io.pathtoolongexception . The code block does not display a warning (although there are long paths) nor log it to a file.

catch [system.io.pathtoolongexception] {
            write-warning $("path is too long")
            write-error-log -message $_.exception.message -severity 3
}

What am I missing?

@motivatedgorilla Welcome to PowerShell.org forums.

is write-error-log your custom function ? Code will enter this catch block only if the exception thrown is pathtoolongexception. Try without catch block and see what exception its throwing for long paths.

@kvprasoon, thanks. No exception is being thrown for long paths although they exist. If I explicitly include a throw statement, the catch statement returns a message. It however stops the script from running any further i.e. it stops.

You could use the -ErrorAction parameter for the cmdlet you use with the loing path, -ErrorAction Stop. This will make the execution terminate and enter catch block.