Skip to content

Respect CommandType during fuzzy Get-Command lookup#27516

Open
KirtiRamchandani wants to merge 2 commits into
PowerShell:masterfrom
KirtiRamchandani:fix/get-command-fuzzy-commandtype
Open

Respect CommandType during fuzzy Get-Command lookup#27516
KirtiRamchandani wants to merge 2 commits into
PowerShell:masterfrom
KirtiRamchandani:fix/get-command-fuzzy-commandtype

Conversation

@KirtiRamchandani
Copy link
Copy Markdown

PR Summary

Fixes #26592.

When Get-Command discovers fuzzy matches from available modules, it currently records fuzzy scores before applying the normal CommandType and parameter filters. OutputResultsHelper later emits from the score list, so commands filtered out of the accumulated result set can still appear in output.

This change keeps fuzzy scores only for commands that pass the same matching path used for accumulated results. The non-fuzzy module lookup path is unchanged.

PR Context

Get-Command -UseFuzzyMatching -CommandType should return only commands of the requested type.

PR Checklist

  • Make sure all .cs, .ps1 and .psm1 files have the correct copyright header
  • Make sure you are merging your commits to the correct branch
  • Make sure you added tests for your changes
  • Make sure you run the relevant tests successfully

Validation

  • Start-PSBuild -NoPSModuleRestore -CI -SkipExperimentalFeatureGeneration -UseNuGetOrg
  • Start-PSPester -Path test/powershell/Modules/Microsoft.PowerShell.Utility/Get-Command.Tests.ps1 -UseNuGetOrg -ThrowOnFailure -Terse -SkipTestToolBuild -BinDir

Result: Get-Command.Tests.ps1 passed: 23 passed, 0 failed.

Copilot AI review requested due to automatic review settings May 24, 2026 14:19
@KirtiRamchandani KirtiRamchandani requested a review from a team as a code owner May 24, 2026 14:19
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Note

Copilot was unable to run its full agentic suite in this review.

Adds regression coverage and adjusts Get-Command fuzzy matching accumulation so that command filtering (e.g., -CommandType) is respected consistently during fuzzy matches.

Changes:

  • Added a Pester test validating -CommandType Function excludes alias results during fuzzy matching.
  • Updated GetCommandCommand.AccumulateMatchingCommands to apply matching/parameter filters while iterating fuzzy matches, accumulating results and scores in one pass.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
test/powershell/Modules/Microsoft.PowerShell.Utility/Get-Command.Tests.ps1 Adds regression test ensuring -CommandType is honored with -UseFuzzyMatching.
src/System.Management.Automation/engine/GetCommandCommand.cs Refactors fuzzy matching accumulation to filter commands before adding to results/scores.

Comment on lines +34 to +47
function Invoke-ZzqFuzzyCommandTypeThing { }
Set-Alias -Name Invoke-ZzqFuzzyCommandTypeThang -Value Invoke-ZzqFuzzyCommandTypeThing

try {
$cmds = Get-Command Invoke-ZzqFuzzyCommandTypeThng -UseFuzzyMatching -CommandType Function

$cmds.Name | Should -Contain 'Invoke-ZzqFuzzyCommandTypeThing'
$cmds.Name | Should -Not -Contain 'Invoke-ZzqFuzzyCommandTypeThang'
$cmds.CommandType | Should -Not -Contain ([System.Management.Automation.CommandTypes]::Alias)
}
finally {
Remove-Item -Path Function:\Invoke-ZzqFuzzyCommandTypeThing -ErrorAction SilentlyContinue
Remove-Item -Path Alias:\Invoke-ZzqFuzzyCommandTypeThang -ErrorAction SilentlyContinue
}
Comment on lines +899 to +913
CommandInfo current = commandScore.Command;

if (IsCommandMatch(ref current, out isDuplicate) && (!IsCommandInResult(current)) && IsParameterMatch(current))
{
_accumulatedResults.Add(current);
_commandScores.Add(new CommandScore(current, commandScore.Score));

commands = _commandScores.Select(static x => x.Command);
// Make sure we don't exceed the TotalCount parameter
++count;

if (TotalCount >= 0 && count >= TotalCount)
{
break;
}
}
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 -CommandType isn't respected when -UseFuzzyMatching is specified

2 participants