Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 554969
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T11:45:59+00:00 2026-05-13T11:45:59+00:00

filter CountFilter($StartAt = 0) { Write-Output ($StartAt++) } function CountFunction { [CmdletBinding()] param (

  • 0
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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T11:46:00+00:00Added an answer on May 13, 2026 at 11:46 am

    This came up on the MVP mail list. It seems that with adv functions, PowerShell is rebinding (reevaluating) the default value each time a pipeline object is received. The folks on the list considered this a bug. Here’s a workaround:

    function CountFunction 
    { 
        [CmdletBinding()] 
        param ( 
            [Parameter(ValueFromPipeline=$true, Mandatory=$true)] 
            $InputObject, 
    
            [Parameter(Position=0)] 
            $StartAt
        )
    
        begin 
        {
            $cnt = if ($StartAt -eq $null) {0} else {$StartAt}
        }
    
        process  
        {  
            Write-Output ($cnt++)
        } 
    } 
    
    $fiveThings = dir | select -first 5  # or whatever 
    
    "Ok" 
    $fiveThings | CountFunction 0 
    
    "FIXED" 
    $fiveThings | CountFunction
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Filter function returns a sublist of elements which return true for a given function.
filter: function(t){ return /^@\w+/.test(t.tweet_raw_text); }, If this JS returns tweets that start with an
To filter a table output of selected entries from a single table i would
Is filter/map equivalent to list comprehension? Suppose I have the following function def fib_gen():
I would like to filter an array of items by using the map() function.
filter -n function(file) { return file.owner == john; } should be parsed into the
My filter mapping in web.xml is as follows: <filter> <filter-name>LoginCheckFilter</filter-name> <filter-class>com.tutorial.filter.LoginCheckFilter</filter-class> </filter> <filter-mapping> <filter-name>LoginCheckFilter</filter-name>
Silverlight filter/class for cloud generation? Some HLSL or just C# class for generating clouds?
i filter the intent like this <intent-filter> <action android:name=android.nfc.action.NDEF_DISCOVERED/> <category android:name=android.intent.category.DEFAULT/> <data android:mimeType=text/plain />
I've this servlet filter servlet called everytime a file from Restricted folder is called.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.