$PSDefaultParameterValues should be automatically cleared in module scope (for all module types: script, manifest, and binary)
Votes from Connect: 4
Original Date Submitted: 4/6/2015 10:06:18 AM
Handle: Kirk Munro
Site Name: PowerShell
Feedback ID: 1228413
While the $PSDefaultParameterValues variable offers many convenient use cases to control how individual or groups of commands behave, it should be automatically cleared in a module scope. Otherwise state configuration that a user sets up in the current runspace may break functionality in modules that are used in that runspace.
For example, June sent out a tweet suggesting you should consider forcing Get-Help to always show help in a window, like this:
$PSDefaultParameterValues['Get-Help:ShowWindow'] = $true
That suggestion is unwise today because any commands that internally perform automation using Get-Help will now apply the ShowWindow switch parameter (if the appropriate parameter set is being used). As a result, my Get-Snippet command from my SnippetPx module appeared broken for a user to followed June's suggestion, just because I didn't explicitly use the equivalent of -ShowWindow:$false in my binary module where Get-Snippet is implemented.
Since PSDefaultParameterValues automatically carries into Module scope today, it puts a lot of onus on the module author to write their module such that it has appropriate defenses against this, so that commands they write work as they were intended to work. Instead of pushing this task upon module authors, it would be much better for the PowerShell ecosystem if PSDefaultParameterValues simply did not carry over into the module scope as it does today.
The workaround for this is one or more of the following:
1. In a script module, invoke $PSDefaultParameterValues.Clear() at the top of the script module. Unfortunately, this should be done in every script module that exists to properly contain PSDefaultParameterValues behaviour. Also unfortunately, this workaround does not resolve the issue for nested binary modules.
2. If you are using a binary module, and if you invoke commands via the System.Management.Automation.PowerShell class, make sure that you either explicitly work around breaking scenarios by passing in parameters that you don't need (yuck) with values such that they won't break your module, or make sure that when you don't use the AddScript method, and if you do use the AddCommand method, do it with the CommandMetadata overload so that your command invocation won't be affected by PSDefaultParameterValues.
Having to keep track of these details and apply them time and again is just too much work for the developer, so please change this behaviour so that commands can work as they were designed to work. PSDefaultParameterValues should be limited to one scope area at a time (user session scope, or scope in a specific module).
Product Studio item created by Connect Synchronizer due to creation of feedback ID 1228413 (http://connect.microsoft.com/PowerShell/feedback/ViewFeedback.aspx?FeedbackID=1228413).
Internal BugId: 14364