Skip to content

Fix Get-Command handling of duplicate command names - #28587

Open
cshaff11 wants to merge 1 commit into
PowerShell:masterfrom
cshaff11:fix/get-command-duplicate-handling
Open

cshaff11 wants to merge 1 commit into
PowerShell:masterfrom
cshaff11:fix/get-command-duplicate-handling

Conversation

@cshaff11

@cshaff11 cshaff11 commented Dec 4, 2026 •

Copy link
Copy Markdown

PR Summary

Get-Command now filters duplicate results consistently, regardless of the input, the type of the command that was found, or the source of the command, and it no longer reports an error for a command that was found.

PR Context

Get-Command has logic to filter out duplicate results, but the result depended on the type of the found command, on whether the command was from a module, and on whether the input was provided by parameter or by pipeline:

  • Duplicate results were not filtered for an Alias or a Function that is not from a module, and for a Cmdlet, ExternalScript or Application that is not from a module when the input was piped in.
  • Duplicate results were filtered, but a non-terminating error was emitted for each duplicate, when the command was an Alias or a Function from a module. The error reported that the command was not recognized, even though it had been found.
PS> Get-Command -Name help, help
CommandType     Name
-----------     ----
Function        help
Function        help

PS> Get-Command -Name PSConsoleHostReadLine, PSConsoleHostReadLine
Get-Command: The term 'PSConsoleHostReadLine' is not recognized as a name of a cmdlet, function, script file, or executable program.

The changes are:

  • The result is now set when a command is found, and not only when it is added to the result. This prevents the 'command not found' error for a command that was found.
  • Aliases and functions now have a key that identifies them, so that duplicates are filtered for them as well.
  • Commands that do not belong to a module are now compared with the commands that are already in the result, so that duplicates are filtered when the input is piped in and the search is repeated.
  • The key of a command uses the name, the type and the identity of the module, so that the same command is recognized when it is reported by different sources, for example by the session state and by the module table. This also makes the result independent of the order of globbed and non-globbed input.

As a result of the last change, Get-Command -All no longer reports the same command more than once when it is found by more than one source, for example when it is reported by both the session state and by the module table.

Fixes #24717

PR Checklist

`Get-Command` has logic to filter out duplicate results, but the result depended on
the type of the command that was found, on whether the command was from a module,
and on whether the input was provided by parameter or by pipeline:

* Duplicate results were not filtered for aliases and functions that are not from
  a module, and for cmdlets, external scripts and applications that are not from
  a module when the input was piped in.
* Duplicate results were filtered, but a non-terminating error was emitted for
  each duplicate, when the command was an alias or a function from a module. The
  error reported that the command was not recognized, even though it had been
  found.

The changes are:

* The result is now set when a command is found, and not only when it is added to
  the result. This prevents the 'command not found' error for a command that was
  found.
* Aliases and functions now have a key that identifies them, so that duplicates
  are filtered for them as well.
* Commands that do not belong to a module are now compared with the commands that
  are already in the result, so that duplicates are filtered when the input is
  piped in and the search is repeated.
* The key of a command uses the name, the type and the identity of the module, so
  that the same command is recognized when it is reported by different sources,
  for example by the session state and by the module table.

Fixes PowerShell#24717
@cshaff11
cshaff11 requested a review from a team as a code owner December 4, 2026 06:49
Copilot AI balanced review requested due to automatic review settings December 4, 2026 06:49
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
There may be pipelines that require an authorized user to comment /azp run to run.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Duplicate suppression can expose shadowed commands and incorrectly discard distinct dynamic-module exports or valid parameter-matched commands.

3 open issues
What changed in this PR

Updates PowerShell’s Get-Command duplicate handling to produce consistent results and avoid false “command not found” errors, addressing #24717.

Changes:

  • Adds command identity keys and duplicate checks across pipeline records.
  • Separates command discovery from result insertion.
  • Adds tests for repeated names, pipeline input, and wildcard ordering.
File Description
test/​powershell/​Modules/​Microsoft.PowerShell.Core/​Get-Command.Tests.ps1 Adds duplicate-handling regression tests.
src/​System.Management.Automation/​engine/​GetCommandCommand.cs Updates command identity, duplicate filtering, and found-result tracking.

Review effort: Balanced

resultFound = true;
}

if (tempResultFound && (!IsCommandInResult(current)))
Comment on lines +1241 to +1243
functionInfo.Name,
":",
GetModuleKey(functionInfo));
}

return result;
return string.Concat(module.Name, "|", module.Guid, "|", module.Version);
@cshaff11

cshaff11 commented Dec 4, 2026

Copy link
Copy Markdown
Author

@microsoft-github-policy-service agree

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Get-Command handles duplicate command name input inconsistently

2 participants