Skip to content

Cmdlets may ignore parameter aliases in case of naming collisions with other parameters; e.g., "Get-ChildItem -d" #4739

Closed

Description

A parameter alias is designed to give an unambiguous alternative name - typically shorter than the original name - to a parameter.

For instance, Get-ChildItem's -Directory parameter (switch) has two aliases: -ad and -d.

Thus, both Get-ChildItem -ad and Get-ChildItem -d should work the same as Get-ChildItem -Directory.

However, Get-ChildItem -d is currently misinterpreted as the -Depth parameter.

The same problem may affect other cmdlets analogously.

Steps to reproduce

Get-ChildItem -ad
Get-ChildItem -d

Expected behavior

Both commands should list the subdirectories of the current directory.

Actual behavior

Get-ChildItem -ad succeeds, but Get-ChildItem -d fails with the following error:

Get-ChildItem : Missing an argument for parameter 'Depth'. Specify a parameter of type 'System.UInt32' and try again.

Environment data

PowerShell Core v6.0.0-beta.6 on macOS 10.12.6
PowerShell Core v6.0.0-beta.6 on Ubuntu 16.04.3 LTS
PowerShell Core v6.0.0-beta.6 on Microsoft Windows 10 Pro (64-bit; v10.0.15063)
Windows PowerShell v5.1.15063.502 on Microsoft Windows 10 Pro (64-bit; v10.0.15063)

Activity

  1. added
    WG-Engine
    Issue-Discussion
    on Sep 4, 2017
  2. PetSerAl commented on Apr 7, 2019

    @PetSerAl
    Contributor

    -Directory is not a parameter of Get-ChildItem cmdlet, while -Depth is. -Directory is dynamic parameter of FileSystem provider. Thus when PowerShell bind static parameters of Get-ChildItem cmdlet -d is unambiguously resolved to -Depth. PowerShell does not try to find better match for already bound static parameters.

    function f {
        [CmdletBinding()]
        param($A1)
        dynamicparam {
            Write-Host "A1 paramter have value '$A1'"
            $DynParams = [System.Management.Automation.RuntimeDefinedParameterDictionary]::new()
            $A2Param = [System.Management.Automation.RuntimeDefinedParameter]::new('A2', [object], ([Parameter]::new(), [Alias]::new('A', 'B')))
            $DynParams.Add('A2', $A2Param)
            $DynParams
        }
        end {
            "A1 parameter: '$A1'"
            "A2 parameter: '$($A2Param.Value)'"
        }
    }
    f -A 42
    f -B 42
  3. mklement0 commented on Apr 7, 2019

    @mklement0
    ContributorAuthor

    As usual, thanks for digging deeper, PetSerAl.

    Given the current behavior, the actionable conclusions are:

    • Remove the d alias from the Directory parameter declaration, because it is effectively inaccessible.

    • Update the documentation accordingly.

    Leaving it at that is an option, but the bigger question is:

    Can dynamic parameters be considered alongside the static ones when looking for ambiguity / unambiguous aliases?

    From a UX perspective, this seems preferable.

    Or are there good (a) conceptual or (b) technical reasons not to do that?

  4. mklement0 commented on Feb 3, 2021

    @mklement0
    ContributorAuthor

    Closed in favor of #14700.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Issue-DiscussionWG-Engine

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions