In working on some script based cmdlets I noticed some inconsistent behavior related to the Preference variables. The $pscmdlet automatic variable provides several methods for interacting with a pipeline, including it’s own WriteDebug function which performs similarly to the Write-Debug cmdlet. Supposedly it respects the $DebugPreference variable the same way as Write-Debug does, but in my testing I discovered this not to be the case.
Given the following example code, If you run the script with a -debug arguement it should output two lines of debug output without halting the script for permission to continue (the default action when -debug is specified is ‘Inquire’).
Note that if you change a preference variable inside of a script block, it will reset when that script block is completed which is why you have to set the value in the BEGIN and PROCESS block.