There is a variable named $VerbosePreference you can check to see how Verbose output should be handled. However, scripts loaded into a separate scope is giving you the issues. If you read the Get-Help about_scopes
, you'll see:
Script:
The scope that is created while a script file runs. Only the commands in the script run in the script scope. To the commands in a script, the script scope is the local scope.
You can add the script to the current scope instead using dot source notation. From the same help file, below the heading Using Dot Source Notation with Scope it is stated that:
Scripts and functions follow all the rules of scope. You create them in a particular scope, and they affect only that scope unless you use a cmdlet parameter or a scope modifier to change that scope.
But, you can add a script or function to the current scope by using dot source notation. Then, when a script runs in the current scope, any functions, aliases, and variables that the script creates are available in the current scope.
I suggest reading up more about scopes in the Get-Help about_scopes
help chapter.
For a quick test of whether this works or not:
[CmdletBinding()]
PARAM()
New-Module -Name TempModule -ScriptBlock {
function Show-ModuleVerbosePreference
{
[CmdletBinding()]
PARAM()
Write-Host "Verbose preference in module function: $VerbosePreference"
}
} | Out-Null
function Show-ScriptVerbosePreference
{
[CmdletBinding()]
PARAM()
Write-Host "Verbose preference in script function: $VerbosePreference"
}
Show-ScriptVerbosePreference
Show-ModuleVerbosePreference</pre>
And if we try to call this script file using different methods we get the following output:
PS C:\> .\verbosity.ps1
Verbose preference in script function: SilentlyContinue
Verbose preference in module function: SilentlyContinue
PS C:\> .\verbosity.ps1 -Verbose
VERBOSE: Exporting function 'Show-ModuleVerbosePreference'.
VERBOSE: Importing function 'Show-ModuleVerbosePreference'.
Verbose preference in script function: Continue
Verbose preference in module function: SilentlyContinue
PS C:\> . .\verbosity.ps1
Verbose preference in script function: SilentlyContinue
Verbose preference in module function: SilentlyContinue
PS C:\> . .\verbosity.ps1 -Verbose
VERBOSE: Exporting function 'Show-ModuleVerbosePreference'.
VERBOSE: Importing function 'Show-ModuleVerbosePreference'.
Verbose preference in script function: Continue
Verbose preference in module function: Continue
So by using dot source notation we have added the script scope into the current scope which seems to make the VerbosePreference setting visible in the module method as well.