Import preference variables from the caller of a powershell module script. This was inspired by The Scripting Guys. Their blog also provides a good overview of the topic and its background.
At the beginning of your powershell module script function invoke Import-CallerPreference
:
Import-Module import-callerpreference
# …
function Receive-PreferencesExample {
[CmdletBinding()]
Param()
Import-CallerPreference
# …
}
Import-CallerPreference
copies the following proference variables from the caller's scope into the current scope:
- ConfirmPreference
- DebugPreference
- ErrorActionPreference
- ErrorView
- FormatEnumerationLimit
- InformationPreference
- LogCommandHealthEvent
- LogCommandLifecycleEvent
- LogEngineHealthEvent
- LogEngineLifecycleEvent
- LogProviderLifecycleEvent
- LogProviderHealthEvent
- OFS
- OutputEncoding
- ProgressPreference
- PSDefaultParameterValues
- PSEmailServer
- PSModuleAutoLoadingPreference
- PSSessionApplicationName
- PSSessionConfigurationName
- PSSessionOption
- VerbosePreference
- WarningPreference
- WhatIfPreference