Skip to content

Add a $PSAssignedParameters automatic variable #20484

@DevOpsJeremy

Description

@DevOpsJeremy

Summary of the new feature / enhancement

There is currently a $PSBoundParameters automatic variable which includes any parameters/values passed to the command. But this variable doesn't include parameters which have default values. There are times when users want any assigned parameter key/values passed to another function--including any default values if the user didn't pass a value to that parameter. Basically any parameter variables which hold a value, whether that value was provided by the user or a default value.

I'd like to propose a $PSAssignedParameters automatic variable which includes any parameters with assigned values, whether they be bound parameters passed by the user, or parameters not passed by the user, but with default values.

Current functionality:

PS > function Get-BoundParameters {
    param (
        $Param1 = "This is param1",
        $Param2,
        $Param3
    )
    [PSCustomObject] $PSBoundParameters
}
PS > Get-BoundParameters -Param2 "This is param2"

Param2
------
This is param2

Currently, only $Param2 is returned, whereas the desired result (for the proposed $PSAssignedParameters variable) is that both $Param1 and $Param2 are returned, since both variables have assigned values--$Param1 from the default value, and $Param2 from the value passed by the user.

Proposed technical implementation details (optional)

Desired functionality:

PS > function Get-AssignedParameters {
    param (
        $Param1 = "This is param1",
        $Param2,
        $Param3
    )
    [PSCustomObject] $PSAssignedParameters
}
PS > Get-AssignedParameters -Param2 "This is param2"

Param1         Param2
------         ------
This is param1 This is param2

The $PSAssignedParameters automatic variable would hold any parameters which have assigned values, whether that value was assigned by the user during runtime, or it's a default value. In this example, it returns $Param1 with the default value and $Param2 with the user-provided value, but doesn't return $Param3 because it doesn't have a value at all.

Possible challenges / considerations

  • How to approach switch/boolean parameters. If the switch/boolean is unused, should it be included in the $PSAssignedParameters hash with a $false value, or should it not be included at all?
  • How to handle dynamic parameters

Metadata

Metadata

Assignees

No one assigned

    Labels

    Issue-Enhancementthe issue is more of a feature request than a bugResolution-DuplicateThe issue is a duplicate.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions