How does a cmdlet know when it really should call WriteVerbose()?

Posted by Roman Kuzmin on Stack Overflow See other posts from Stack Overflow or by Roman Kuzmin
Published on 2010-05-05T17:18:55Z Indexed on 2010/05/05 22:38 UTC
Read the original article Hit count: 200

Filed under:

How does a cmdlet know when it really should call WriteVerbose(), WriteDebug() and etc.?

Perhaps I miss something simple but I cannot find the answer. All cmdlet implementations I have seen so far just call WriteVerbose() without any hesitation. I know that it is correct to do so, but it is not effective.

Performance suffers when verbose mode is off but a cmdlet still prepares data for WriteVerbose() call, that is, for nothing.

In other words, in a cmdlet I would like to be able to:

if (<VerboseMode>)
{
    .... data preparation, sometimes expensive ...
    WriteVerbose(...);
}

But I don't know how to get this if (<VerboseMode>). Any ideas?

© Stack Overflow or respective owner

Related posts about powershell