Powershell advanced functions: are optional parameters supposed to get initialized?

Posted by Richard Berg on Stack Overflow See other posts from Stack Overflow or by Richard Berg
Published on 2010-01-22T16:42:10Z Indexed on 2010/04/27 3:33 UTC
Read the original article Hit count: 405

filter CountFilter($StartAt = 0) 
{ 
    Write-Output ($StartAt++) 
}

function CountFunction
{
    [CmdletBinding()]
    param (
        [Parameter(ValueFromPipeline=$true, Mandatory=$true)]
        $InputObject,
        [Parameter(Position=0)]
        $StartAt = 0
    )

    process 
    { 
        Write-Output ($StartAt++) 
    }
}

$fiveThings = $dir | select -first 5  # or whatever

"Ok"
$fiveThings | CountFilter 0

"Ok"
$fiveThings | CountFilter

"Ok"
$fiveThings | CountFunction 0

"BUGBUG ??"
$fiveThings | CountFunction

I searched Connect and didn't find any known bugs that would cause this discrepancy. Anyone know if it's by design?

© Stack Overflow or respective owner

Related posts about powershell

Related posts about function