Repository navigation
Conversation
`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
|
Azure Pipelines: There may be pipelines that require an authorized user to comment /azp run to run. |
Contributor
There was a problem hiding this comment.
🟡 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); |
Author
|
@microsoft-github-policy-service agree |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

PR Summary
Get-Commandnow 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-Commandhas 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:Aliasor aFunctionthat is not from a module, and for aCmdlet,ExternalScriptorApplicationthat is not from a module when the input was piped in.Aliasor aFunctionfrom a module. The error reported that the command was not recognized, even though it had been found.The changes are:
As a result of the last change,
Get-Command -Allno 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
.h,.cpp,.cs,.ps1and.psm1files have the correct copyright header