Writing Functions with Advanced Lifecycle

suggest change

This example shows how a function can accept pipelined input, and iterate efficiently.

begin and end are optional. process is required when the parameter uses ValueFromPipeline or ValueFromPipelineByPropertyName. Without process, the function body is an end block and only the last pipeline object is bound.

function Write-FromPipeline{
    [CmdletBinding()]
    param(
        [Parameter(ValueFromPipeline)]
        $myInput
    )
    begin {
        Write-Verbose -Message "Beginning Write-FromPipeline" 
    }
    process {
        Write-Output -InputObject $myInput
    }
    end {
        Write-Verbose -Message "Ending Write-FromPipeline"
    }
}

$foo = 'hello','world',1,2,3

$foo | Write-FromPipeline -Verbose
VERBOSE: Beginning Write-FromPipeline
hello
world
1
2
3
VERBOSE: Ending Write-FromPipeline

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents