Skip to content

Fix the description of the -InputObject parameter for Measure-Command #2140

Closed

Description

Currently, -InputObject is described as follows:

Specifies objects representing the expressions to be measured.

The actual behavior is as follows:

Objects bound to -InputObject are optional input for the script block passed to -Expression:

Inside the script block, $_ can be used to reference the input object [at hand], as you would in a script block passed to ForEach-Object.

Note: If you use the pipeline to pass multiple input objects, Measure-Command invokes the -ScriptBlock script block once for each input object.

While Measure-Command still times the overall execution (across multiple script-block invocations, if applicable), the overhead from multiple invocations presumably distorts the timings.

Therefore, if feasible, pass the input objects as an (array) argument; e.g.:

> Measure-Command { "$($_.Count): [$_]" | Out-Default } -InputObject 'hi', 'there'
2: [hi there]
...

That is, the script block is invoked once, and $_ refers to all input objects.

Contrast this with pipeline use:

> 'hi', 'there' | Measure-Command { "$($_.Count): [$_]" | Out-Default }
1: [hi]
1: [there]
...

That is, the script block is invoked once for each input object, and $_ refers to the object at hand.

Note that this use of -InputObject is a reversal of what makes sense with most other cmdlets, where passing multiple objects only makes sense via the pipeline - see PowerShell/PowerShell#4242.

Version(s) of document impacted

  • Impacts 6 document
  • Impacts 5.1 document
  • Impacts 5.0 document
  • Impacts 4.0 document
  • Impacts 3.0 document

Activity

  1. added a commit that references this issue on Jun 19, 2018
    e59bd73
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

Pri3

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions