Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions src/System.Management.Automation/engine/GetCommandCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -935,6 +935,30 @@ private void AccumulateMatchingCommands(IEnumerable<string> commandNames)
}
}

// Command discovery retries noun-only names with the default verb prepended.
// Do the same here after an exact lookup fails.
if (!resultFound && !isDuplicate && !isPattern && !commandName.Contains('-') && !commandName.Contains('\\'))
{
string defaultVerbCommandName =
StringLiterals.DefaultCommandVerb + StringLiterals.CommandVerbNounSeparator + commandName;
string tempCommandName = defaultVerbCommandName;
if (!string.IsNullOrEmpty(moduleName))
{
tempCommandName = moduleName + "\\" + defaultVerbCommandName;
}

try
{
CommandDiscovery.LookupCommandInfo(tempCommandName, this.MyInvocation.CommandOrigin, this.Context);
}
catch (CommandNotFoundException)
{
// Ignore and let the regular Get-Command error handling report the original name.
}

resultFound = FindCommandForName(options, defaultVerbCommandName, isPattern: false, emitErrors: false, ref count, out isDuplicate);
}

// If we are trying to match a single specific command name (no glob characters)
// then we need to write an error if we didn't find it.
if (!isDuplicate)
Expand Down
31 changes: 31 additions & 0 deletions test/powershell/engine/Basic/CommandDiscovery.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,37 @@ Describe "Command Discovery tests" -Tags "CI" {
(& 'location').Path | Should -Be (Get-Location).Path
}

It "Get-Command prepends Get- to noun-only command names" {
function Get-DefaultVerbCommandTest {}

(Get-Command DefaultVerbCommandTest).Name | Should -BeExactly 'Get-DefaultVerbCommandTest'
}

It "Get-Command prefers an exact noun-only command name" {
function DefaultVerbCommandTest {}
function Get-DefaultVerbCommandTest {}

(Get-Command DefaultVerbCommandTest).Name | Should -BeExactly 'DefaultVerbCommandTest'
}

It "Get-Command applies command type filtering before the default verb fallback" {
Set-Alias DefaultVerbCommandTest Get-Date
function Get-DefaultVerbCommandTest {}

(Get-Command DefaultVerbCommandTest -CommandType Function).Name | Should -BeExactly 'Get-DefaultVerbCommandTest'
}

It "Get-Command does not prepend Get- to wildcard command names" {
function Get-DefaultVerbCommandTest {}

Get-Command 'DefaultVerbCommand*' | Should -BeNullOrEmpty
}

It "Get-Command reports the original noun-only name when no command is found" {
{ Get-Command DefaultVerbMissingCommandTest -ErrorAction Stop } |
Should -Throw -ErrorId 'CommandNotFoundException,Microsoft.PowerShell.Commands.GetCommandCommand'
}

Context "Use literal path first when executing scripts" {
BeforeAll {
$firstFileName = '[test1].ps1'
Expand Down