PowerShell module function does not inherit WhatIf PowerShell module function does not inherit WhatIf powershell powershell

PowerShell module function does not inherit WhatIf


Caller Preference Variables are not propagated from Call-ShowWhatIf to Show-WhatIfOutput. AFAIK this is a known issue with functions called from a module. In this case, it's the $WhatIfPreference that is not being propagated.

Adding this to your Show-WhatIfOutput should resolve the issue:

if (-not $PSBoundParameters.ContainsKey('WhatIf')){    $WhatIfPreference= $PSCmdlet.GetVariableValue('WhatIfPreference')}

It checks if the caller (Call-ShowWhatIf) has -WhatIf specified. It does this when -WhatIf is not specified in the function call (Show-WhatIfOutput)

This is a similar post which described the same issue with -Verbose not propagating.

Related links:
PowerShell.org - Script Modules and Variable Scopes
Scripting Guy - Weekend Scripter: Access PowerShell Preference Variables
TechNet - Import Preference variables from the caller of a Script Module function