1

After supplying the values it keeps asking for the next if the required position takes an array ,how do I tell I am done. thank you. example

"hello worll world"|select-string
Supply values for the following parameters:
Pattern[0]:"hello"
Pattern[1]:

what I want provide pattern[0] and get the result.

Mathias R. Jessen
  • 157,619
  • 12
  • 148
  • 206
Tes Dan
  • 11
  • 1

1 Answers1

2

Whenever a call to a PowerShell command doesn't supply arguments (values) to all of its mandatory parameters (parameters that must be given values in order for the command to operate):

  • PowerShell by design interactively prompts for these arguments, and only if you supply all required arguments (using valid values) does the command actually execute.

  • For array-typed mandatory parameters, such as Select-String's -Pattern parameter - which by definition accept an open-ended number of values - the end of the interactive value input must be signaled by submitting an empty value.

    • In practical terms: After typing the last (and possibly only) actual value, press Enter twice, as Mathias notes.

    • However, you appear to have found a bug with Select-String's -Pattern parameter, specifically: a spurious error occurs when you use the technique above:

      • The input object cannot be bound to any parameters for the command either because the command does not take pipeline input or the input and its properties do not match any of the parameters that take pipeline input

        • The only way to avoid the bug is to avoid pipeline input, by supplying the input string via the -InputObject parameter, which, however, only works as intended with a single (potentially multiline) input string:

           Select-String -InputObject 'hello world'
          
      • The bug affects both Windows PowerShell and PowerShell (Core) up to at least v7.3.6; while Windows PowerShell will see only critical fixes going forward, there's a good chance the bug will be fixed in a future PowerShell (Core) version: the relevant bug report is GitHub issue #19986.

Note:

  • This automatic prompting mechanism doesn't work with all parameter types, and generally provides a poor end-user experience - see GitHub issue #4068.

  • For commands you author yourself, you can avoid this mechanism by making it throw an error in case mandatory arguments are missing - see this answer, which also discusses how you can discover a given command's mandatory parameters.

mklement0
  • 382,024
  • 64
  • 607
  • 775