PowerShell: Define Function to Accept Pipe

By Xah Lee. Date: .

to write a function that takes piped input, use one of the following in function body:

Begin {statements}

Runs once when the function begins to run.

Process {statements}

Runs once for each piped object. The current piped object is represented by $_

function f { process { "yay " + $_ } }

dir | f
# prints yay full path
End {statements}

Runs once when all piped object are received.

If the function contains any of these block, any code outside are ignored.

If a function does not contain any of these blocks, the function behaves the same as if all statements are in End block.

PowerShell: Define Function