Verbose cmdletbinding not passing to module functions

This topic contains 3 replies, has 3 voices, and was last updated by Profile photo of Adam Weigert Adam Weigert 1 year, 2 months ago.

  • Author
  • #28755
    Profile photo of Adam Weigert
    Adam Weigert

    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
  • #28756
    Profile photo of Don Jones
    Don Jones

    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?

  • #28757
    Profile photo of Dave Wyatt
    Dave Wyatt

    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

  • #28758
    Profile photo of Adam Weigert
    Adam Weigert

    Thanks Dave, that's exactly it.

You must be logged in to reply to this topic.