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 518059
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T07:53:54+00:00 2026-05-13T07:53:54+00:00

# array C:\> (1,2,3).count 3 C:\> (1,2,3 | measure).count 3 # hashtable C:\> @{1=1;

  • 0
# array
C:\> (1,2,3).count
3
C:\> (1,2,3 | measure).count
3

# hashtable
C:\> @{1=1; 2=2; 3=3}.count
3
C:\> (@{1=1; 2=2; 3=3} | measure).count
1

# array returned from function
C:\> function UnrollMe { $args }
C:\> (UnrollMe a,b,c).count
3
C:\> (UnrollMe a,b,c | measure).count
1
C:\> (1,2,3).gettype() -eq (UnrollMe a,b,c).gettype()
True

The discrepancy with HashTables is fairly well known, although the official documentation only mentions it obliquely (via example).

The issue with functions, though, is news to me. I’m kind of shocked it hasn’t bitten me before now. Is there some guiding principle we scripters can follow? I know that when writing cmdlets in C# there’s an overload of WriteObject where you can control enumeration explicitly, but AFAIK there’s no such construct in the Posh language itself. As the final example shows, the Posh interpreter seems to believe there is no difference in the type of objects being piped. I suspect there may be some Object vs PSObject weirdness under the hood, but that’s of little use when you’re writing pure Posh and expect the script language to “just work.”

/ EDIT /

Keith is correct to point out that in my example, I’m passing in a single string[] argument rather than 3 string arguments. In other words, the reason Measure-Object says Count=1 is because it’s seeing a single array-of-arrays whose first element is @(“a”, “b”, “c”). Fair enough. This knowledge allows you to work around the issue in several ways:

# stick to single objects
C:\> (UnrollMe a b c | measure).count
3

# rewrite the function to handle nesting
C:\> function UnrollMe2 { $args[0] }
C:\> (UnrollMe2 a,b,c | measure).count
3

# ditto
C:\> function UnrollMe3 { $args | %{ $_ } }
C:\> (UnrollMe3 a,b,c | measure).count
3

However, it doesn’t explain everything…

# as seen earlier - if we're truly returning @( @("a","b","c") ) why not count=1?
C:\> (UnrollMe a,b,c).count
3

# our theory must also explain these results:
C:\> ((UnrollMe a,b,c) | measure).count
3
C:\> ( @(@("a","b","c")) | measure).count
3
C:\> ((UnrollMe a,b,c d) | measure).count
2

From what I can extrapolate there’s another rule in play: if you have an array with exactly one element AND the parser is in expression mode, then the interpreter will “unwrap” said element. Any more subtleties I’m missing?

  • 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-13T07:53:54+00:00Added an answer on May 13, 2026 at 7:53 am

    $args is unrolled. Remember that function parameters are normally passed using space to separate them. When you pass in 1,2,3 you are passing in a single argument that is an array of three numbers that gets assigned to $args[0]:

    PS> function UnrollMe { $args }
    PS> UnrollMe 1 2 3 | measure
    
    Count    : 3
    

    Putting the results (an array) within a grouping expression (or subexpression e.g. $()) makes it eligible again for unrolling so the following unrolls the object[] containing 1,2,3 returned by UnrollMe:

    PS> ((UnrollMe 1,2,3) | measure).Count
    3
    

    which is equivalent to:

    PS> ((1,2,3) | measure).Count
    3
    

    BTW it doesn’t just apply to an array with one element.

    PS> ((1,2),3) | %{$_.GetType().Name}
    Object[]
    Int32
    

    Using an array subexpression (@()) on something that is already an array has no effect no matter how many times you apply it. 🙂 If you want to prevent unrolling use the comma operator because it will always create another outer array which gets unrolled. Note that in this scenario you don’t really prevent unrolling, you just work around the unrolling by introducing an outer “wrapper” array that gets unrolled instead of your original array e.g.:

    PS> (,(1,2,3) | measure).Count
    1
    

    Finally, when you execute this:

    PS> (UnrollMe a,b,c d) | %{$_.GetType().Name}
    Object[]
    String
    

    You can see that UnrollMe returns two items (a,b,c) as an array and d as a scalar. Those two items get sent down the pipeline separately which is the resulting count is 2.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 306k
  • Answers 306k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You can call the open method (which calls an mx_internal… May 13, 2026 at 9:16 pm
  • Editorial Team
    Editorial Team added an answer answer: if you are getting this error check the service… May 13, 2026 at 9:16 pm
  • Editorial Team
    Editorial Team added an answer Use live event Attach a handler to the event for… May 13, 2026 at 9:16 pm

Related Questions

I'm very new to lambda expressions in C#, and I'm having trouble conceptualizing how
My sql statement is not working with Zend, its complaining about the Count(*) field...
(C#, prime generator) Heres some code a friend and I were poking around on:
Is there a better way to shell sort using C#? // array of integers
Being new to Cocoa, and probably not knowing all of the potential classes available

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.