Skip to content

Document ad-hoc script-block arguments for end users #2338

Closed

Description

See #2427 for the cmdlet author perspective.

Ad-hoc script-block arguments are widely used, but not documented, which makes it hard to infer when and how to use them.

A widely used pattern is to pass a script block to Rename-Item -NewName in order to derive the new name from the input file:

Get-Item *.txt | Rename-Item -NewName { $_.BaseName + '.rtf' } -WhatIf

Specifically, this technique should be explained / mentioned in:

The only existing information I could find is:

Based on my own findings, script-block parameters work as follows:

Ad-hoc Script-Block Arguments are an implicit feature that:

  • [only] works with parameters that are designed to take pipeline input
    • as long as those parameters are NOT typed as one of the following, in which case REGULAR parameter binding happens:
      • [scriptblock]
      • [object] # [psobject], however, DOES work
      • (no type specified) # effectively the same as [object]
  • enables per-input-object transformations via a script block (to which the current pipeline object is passed as $_, as usual)
    • Since such ad-hoc script blocks by definition do not match the type of the parameter you're targeting, you must always use the parameter name explicitly.
    • ScriptBlock Parameters provide unconditional access to the pipeline input object, even if the parameter would ordinarily NOT be bound by a given pipeline object in the absence of the designated property.
    • This enables techniques such as the following call to Rename-Item, where the pipeline input from Get-Item is - as usual - bound to the -LiteralPath parameter, but passing a script block to -NewName - which would ordinarily only bind to input objects with a .NewName property - enables access to the same pipeline object and thus to derive the destination filename from the input filename:
      • Get-Item file | Rename-Item -NewName { $_.Name + '1' } # renames 'file' to 'file1'; input binds to both -LiteralPath (implicitly) and the -NewName script block.
    • The script blocks run in a child variable scope, which means that you cannot directly modify the caller's variables, such as incrementing a counter; as a workaround, use Get-Variable and reference a variable object's .Value property inside the script block - see this SO answer for an example.
    • Error condition: If you pass a script block but do not also provide pipeline input, you'll get the following error:
      • Cannot evaluate parameter 'NewName' because its argument is specified as a script block and there is no input. A script block cannot be evaluated without input.

Version(s) of document impacted

  • Impacts 6.1 document
  • Impacts 6.0 document
  • Impacts 5.1 document
  • Impacts 5.0 document
  • Impacts 4.0 document
  • Impacts 3.0 document

Activity

  1. changed the title [-]Document script-block parameters[/-] [+]Document script-block parameters for end users[/+] on May 13, 2018
  2. changed the title [-]Document script-block parameters for end users[/-] [+]Document ad-hoc script-block arguments for end users[/+] on May 13, 2018
  3. added a commit that references this issue on Jun 25, 2018
    5535a10
  4. bobbytreed commented on Jun 25, 2018

    @bobbytreed
    Contributor

    Closed by #2612

  5. added a commit that references this issue on Jun 25, 2018
    f294f3a
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

    Pri2

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions