Repository navigation
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' } -WhatIfSpecifically, this technique should be explained / mentioned in:
The only existing information I could find is:
-
A comment by Dave Wyatt (@dlwyatt) on a Boe Prox blog post at https://learn-powershell.net/2014/10/11/using-a-scriptblock-parameter-with-a-powershell-function/
-
A blog post by Jeffrey Snover from 2006, which, however, is partly obsolete, because it claims that the feature works specifically and only with parameters that are NOT designed to take pipeline input: https://blogs.msdn.microsoft.com/powershell/2006/06/23/flexible-pipelining-with-scriptblock-parameters/ - however, since (at least) PSv2 it works only for parameters that ARE designed for pipeline input.
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]
- as long as those parameters are NOT typed as one of the following, in which case REGULAR parameter binding happens:
- 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 fromGet-Itemis - as usual - bound to the-LiteralPathparameter, but passing a script block to-NewName- which would ordinarily only bind to input objects with a.NewNameproperty - 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-NewNamescript 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-Variableand reference a variable object's.Valueproperty 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
[-]Document script-block parameters[/-][+]Document script-block parameters for end users[/+][-]Document script-block parameters for end users[/-][+]Document ad-hoc script-block arguments for end users[/+]Closes MicrosoftDocs#2338 - Adds documentation for delay-bind script …
bobbytreed commentedon Jun 25, 2018
Closed by #2612
Closes #2338 - Adds documentation for delay-bind script block paramet…