Verbose cmdletbinding not passing to module functions

It appears that when I call the script with -Verbose, that the CmdletBinding typically applies to functions called that have the CmdletBinding attribute as well, but when I use Import-Module with a path to a psm1 file it does not pass the Verbose parameter to the functions exported in that module.



Import-Module $PSScriptRoot\Module.psm1 -Verbose:$false



function Test-Verbose

    Write-Verbose "Verbose is enabled"

Export-ModuleMember -Function *
.\Script.ps1 -Verbose

The script will not display verbose output and the $VerbosePreference is reported as SilentlyContinue.

If I change Script.ps1 to call Test-Verbose -Verbose:$VerbosePreference it works like CmdletBinding is enabled.

So what is different with a module loaded by path versus loaded by name.


Name                           Value                                                                                                                                                                                                                             
----                           -----                                                                                                                                                                                                                             
PSVersion                      4.0                                                                                                                                                                                                                               
WSManStackVersion              3.0                                                                                                                                                                                                                               
CLRVersion                     4.0.30319.34209                                                                                                                                                                                                                   
BuildVersion                   6.3.9600.17400                                                                                                                                                                                                                    
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0}                                                                                                                                                                                                              
PSRemotingProtocolVersion      2.2

So - maybe this is just your example here? - you’ve shown a .ps1 file, not a script module. Did you indeed test this with a .psm1? And the only difference was, in one case you loaded it by path, and in another case you had it in PSModulePath?

That’s what led me to write the Get-CallerPreference command. The main problem is that variables in the caller’s scope (when the caller is in a different script module / session state, and the caller’s scope is not Global) are not inherited by the module. See for more information (and a link to the module on the TechNet Gallery).

You can also install it with PowerShellGet: Install-Module PreferenceVariables

Thanks Dave, that’s exactly it.