Powershell $Error object not immediately populating inside PSM1 module Powershell $Error object not immediately populating inside PSM1 module powershell powershell

Powershell $Error object not immediately populating inside PSM1 module


As Ethan mentioned, you need to look at $global:error to see all of the errors. Modules always have they're own $error variable but it's (mostly) not used.

background: at one point, we had considered isolating errors that occurred in module code to the module scope but ultimately decided to add all errors to the global error collection since it is essentially a log of errors (like an in-memory eventlog.) Unfortunately the module scoped $error wasn't removed before the release and so you need to use the global scope qualifier to access the "real" $error variable.

Bruce Payette, Microsoft Corporation


I hadn't noticed that before, but perhaps the $error collection is scoped to the module like any other variables. Try comparing the values of the following two explicitly scoped variables at key points in your test script:

"Errors - Global: {0}; Module: {1}" -f $global:error.count, $script:error.count

Let me know how you get on.