Inheriting Verbose Preference in Windows PowerShell Module Functions

When specifying verbose preference this will only be inherited by the scripts module functions, if specified in the global scope.

However, it is possible to use an advanced function to access the variable preference using the ‘$PSCmdlet.GetVariableValue()’ method to access the SessionState object and inherit the preference value.


[CmdletBinding()]
Param (
$VerbosePreference = $PSCmdlet.GetVariableValue('VerbosePreference')
)

The important part of the above is that the parameter block contains [CmdletBinding()]  attributes, without this the $PSCmdlet value will be null.

 


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s