Author Posts

August 19, 2015 at 5:28 am

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.

[Script.ps1]

[CmdletBinding()]
param()

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

Test-Verbose

[Module.psm1]

function Test-Verbose
{
    [CmdletBinding()]
    param()

    $VerbosePreference
    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.

$PSVersionTable

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

August 19, 2015 at 5:38 am

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?

August 19, 2015 at 6:13 am

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 http://blogs.technet.com/b/heyscriptingguy/archive/2014/04/26/weekend-scripter-access-powershell-preference-variables.aspx for more information (and a link to the module on the TechNet Gallery).

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

August 19, 2015 at 6:41 am

Thanks Dave, that's exactly it.