I have written my own Powershell logging function Log
with parameters stream
(on which stream to write the message) and message
(the messa
Came across this looking for the same answer and found some good info, also some not so good. The marked answer seems to be dated and not correct as the comments stated. The PSBoundParameter property object from the MyInvocation object is a Dictionary (PoSH 5.1 up maybe earlier didn't check) which does not contain the IsPresent property. The asker also forgot to consider $VerbosePreference where other answers have presented this option.
Here's a solution that makes it simple and easy:
if ( $PSBoundParameters['Verbose'] -or $VerbosePreference -eq 'Continue' ) {
# do something
}
$PSBoundParameters is a hashtable object, if the value is present and true it will evaluate to true, if it is not present or present and not true it will evaluate to false. VerbosePreference which is set at the session level will display verbose statements when the value is Continue. Put this together in a condition using a logical or and you will get an accurate representation if verbose output is desired.