Skip to content

Get-Command " " Crashes with IndexOutOfRangeException #10148

Closed

Description

@lordmilko

Running Get-Command on a string containing only spaces results in the cmdlet crashing with an IndexOutOfRangeException rather than throwing a proper exception (such as a CommandNotFoundException) describing what was wrong with your input.

Steps to reproduce

Get-Command " "

Expected behavior

A CommandNotFoundException specifying that the term is not recognized as the name of a cmdlet, function, script file or operable program

Actual behavior

An IndexOutOfRangeException due to an explosion in CommandSearcher.DoPowerShellRelativePathLookup

C:\> Get-Command " "
Get-Command : Index was outside the bounds of the array.

Exception stack trace:

   at System.Management.Automation.CommandSearcher.DoPowerShellRelativePathLookup()
   at System.Management.Automation.CommandSearcher.ProcessPathSearchState()
   at System.Management.Automation.CommandSearcher.MoveNext()
   at Microsoft.PowerShell.Commands.GetCommandCommand.FindCommandForName(SearchResolutionOptions options, String commandName, Boolean isPattern, Boolean emitErrors, Int32& currentCount, Boolean& isDuplicate)
   at Microsoft.PowerShell.Commands.GetCommandCommand.AccumulateMatchingCommands(IEnumerable`1 commandNames)
   at Microsoft.PowerShell.Commands.GetCommandCommand.AccumulateMatchingCommands()
   at Microsoft.PowerShell.Commands.GetCommandCommand.ProcessRecord()
   at System.Management.Automation.Cmdlet.DoProcessRecord()
   at System.Management.Automation.CommandProcessor.ProcessRecord()

Environment data

Name                           Value
----                           -----
PSVersion                      7.0.0-preview.1
PSEdition                      Core
GitCommitId                    7.0.0-preview.1
OS                             Microsoft Windows 6.1.7601 Service Pack 1
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0.}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

Seems to affect all versions of PowerShell, at the very least going back as far as Windows PowerShell 5.1

Activity

  1. added
    Issue-Question
    on Jul 13, 2019
  2. added
    Issue-Bug
    and removed
    Issue-Question
    on Jul 15, 2019
  3. MartinGC94 commented on Dec 30, 2022

    @MartinGC94
    Contributor

    This doesn't happen on 7.4.0 preview 1, instead I get the error:

    Get-Command: The term ' ' is not recognized as a name of a cmdlet, function, script file, or executable program.
    Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
    

    Like I would expect

  4. ghost closed this as completedon Dec 31, 2022
  5. added
    WG-Cmdlets
    and removed on Jul 17, 2026
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-BugResolution-FixedWG-Cmdlets

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions