Skip to content

Why is Import-Module transparent to $VerbosePreference but not -Verbose? #12516

Closed

Description

@alx9r

I've been reviewing #4568 and related issues trying to understand the design intent of the differences between $VerbosePreference and -Verbose, but I'm coming up short. Import-Module seems to pass $VerbosePreference to the module scriptblock, but the value of the -Verbose parameter does not pass through. On the other hand, the value of -Verbose passes through to a function defined in the module when it is called.

  1. Should the value of -Verbose passed to Import-Module be reflected by $VerbosePreference when the module's scriptblock is executed?
  2. If not, what is the reasoning behind this?
  3. Is there any way to access the value of -Verbose from within the scriptblock imported by Import-Module?

Steps to reproduce

#m.psm1 
$args[0].vp = $VerbosePreference           # "ModulePref"
function ModuleFunction {
    [CmdletBinding()]
    param()
    [pscustomobject]@{
        vp = $VerbosePreference            # "FuncPref","FuncPrefNoArg"
        v  = $PSCmdlet.
                MyInvocation.
                BoundParameters['Verbose'] # "FuncArg"
    }
}
@(
    @{ vp='Continue'         ; v=$true  }
    @{ vp='Continue'         ; v=$false }
    @{ vp='SilentlyContinue' ; v=$true  }
    @{ vp='SilentlyContinue' ; v=$false }
) |
    . {
        process {
        $h= @{}
        $VerbosePreference = $_.vp          # "CallerPref"
        Import-Module $PSScriptRoot\m.psm1 -Force `
                      -args $h `
                      -Verbose:$_.v         # "CallerArg"

        $mf = ModuleFunction -Verbose:$_.v  # "CallerArg"
        [pscustomobject]@{
            CallerPref    = $_.vp
            CallerArg     = $_.v
            ModulePref    = $h.vp
            FuncPref      = $mf.vp
            FuncArg       = $mf.v
            FuncPrefNoArg = ModuleFunction | % vp
        }
    }} | ft

Expected behavior

Note the difference in the "ModulePref" column.

CallerPref       CallerArg       ModulePref         FuncPref FuncArg    FuncPrefNoArg
----------       ---------       ----------         -------- -------    -------------
Continue              True         Continue         Continue True            Continue
Continue             False SilentlyContinue SilentlyContinue False           Continue
SilentlyContinue      True         Continue         Continue True    SilentlyContinue
SilentlyContinue     False SilentlyContinue SilentlyContinue False   SilentlyContinue

Actual behavior

CallerPref       CallerArg       ModulePref         FuncPref FuncArg    FuncPrefNoArg
----------       ---------       ----------         -------- -------    -------------
Continue              True         Continue         Continue True            Continue
Continue             False         Continue SilentlyContinue False           Continue
SilentlyContinue      True SilentlyContinue         Continue True    SilentlyContinue
SilentlyContinue     False SilentlyContinue SilentlyContinue False   SilentlyContinue

Environment data


Name                           Value
----                           -----
PSVersion                      7.1.0-preview.2
PSEdition                      Core
GitCommitId                    7.1.0-preview.2
OS                             Microsoft Windows 6.3.9600
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0


Activity

  1. added
    Issue-Question
    on Apr 28, 2020
  2. added
    WG-Engine
    on Apr 30, 2020
  3. JustinGrote commented on Apr 8, 2021

    @JustinGrote
    Contributor

    It also has different behavior if you use -Force or not. If you use -Force, it will ignore -Verbose setting.

  4. microsoft-github-policy-service commented on Nov 16, 2023

    @microsoft-github-policy-service
    Contributor

    This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.

  5. microsoft-github-policy-service commented on Nov 16, 2023

    @microsoft-github-policy-service
    Contributor

    This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.

  6. added
    Resolution-No Activity
    on Nov 16, 2023
  7. microsoft-github-policy-service commented on Nov 23, 2023

    @microsoft-github-policy-service
    Contributor

    This issue has been marked as "No Activity" as there has been no activity for 6 months. It has been closed for housekeeping purposes.

  8. boernsen-development commented on Jul 8, 2025

    @boernsen-development

    I find this behavior very confusing. Why is it closed? Are there any practicable workarounds?
    Manually passing the "correct" VerbosePreference to every function seems tedious and unnecessarily polutes the code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Issue-QuestionResolution-No ActivityWG-Engine

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions