Add module-level default parameter assignment support to PSDefaultParameterValues - by Kirk Munro

Status : 


Sign in
to vote
ID 844699 Comments
Status Active Workarounds
Type Suggestion Repros 1
Opened 4/1/2014 7:57:59 AM
Access Restriction Public


As PowerShell evolves, more patterns develop.  With those patterns, you may have multiple modules that use the same pattern against different targets. For example, the SMA module has a WebServicesEndpoint common parameter that can be used to remotely manage SMA.  Rather than using a variable or entering this all the time, it is easier to use PSDefaultParameterValues.  Unfortunately, that either means setting the default value for WebServicesEndpoint for all cmdlets (global assignments should be discouraged) or setting it atomically on each cmdlet (which means updated versions of the module with new commands mean you need to revisit the assignments and add some for the new commands).  I'm working on a module that will use WebServicesEndpoint as a parameter as well.  With these patterns in mind, it becomes very important to allow PSDefaultParameterValues to be used to assign a default value to a single parameter across all commands that are part of a specific module.  This way you can avoid the complexity and error that comes with atomic assignments while also avoiding the issues that come with global assignments.

I propose a syntax that leverages the fully-qualified command name format, as follows:

$PSDefaultParameterValues['Microsoft.SystemCenter.ServiceManagementAutomation\*:WebServicesEndpoint'] = ''

This would also fix the issue with fully-qualified command names not being supported by this feature.

Sign in to post a comment.
Posted by Christopher King on 3/14/2017 at 12:41 PM
I agree with the title of this but want to be clear on why I am upvoting. Maybe my idea is not what the original request would address.

By module level. I want to set say computername for any DNS Server cmdlets.

For example, I set this. It would be quite tedious to do this for all cmdlets I care about.

$PSDefaultParameterValues=@{ "Get-DnsServerResourceRecord:ComputerName"="DC_NAME" }
Posted by Kirk Munro on 6/3/2015 at 9:24 AM
I just came across this problem again, with a different use case. I was working on a new module and decided to try using the DefaultCommandPrefix manifest attribute instead of hard-coding the command prefix like I usually do. But for that module, there is a common parameter that identifies an API key (GUID). In order to be able to assign that key once and have it used as the default everywhere, I tried using PSDefaultParameterValues, but since I need to specify a command name so that I don't influence/break other commands that are not in the module, I need to create one entry per command where this is used, and I need to do so using the hard-coded prefix name (which breaks Import-Module -Prefix support). This is another scenario where I would have liked to simply be able to do this:

$PSDefaultParameterValues['LodPx\*:ApiKey'] = $myApiGuid

That solution would have worked across all commands in my module that have an ApiKey parameter, whether the user imported the module with -Prefix to define their own prefix or not.