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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T23:20:00+00:00 2026-05-22T23:20:00+00:00

Forgive the title, I’m not really sure how to explain what I’m seeing. Sample

  • 0

Forgive the title, I’m not really sure how to explain what I’m seeing.

Sample Code:

    $SampleValues = 1..5
    $Result = "" | Select ID
    $Results = @()  

    $SampleValues | %{
        $Result.ID = $_
        $Results += $Result
    }

$Results

This is fairly straightforward:

  • Create an array with 5 numbers to be used in a loop
  • Create a temp variable with a NoteProperty called ID
  • Create an empty array to store results
  • Iterate through each of the 5 numbers assigning them to a temp variable then appending that to an array.

The expected result is 1,2,3,4,5 but when run this returns 5,5,5,5,5

This is a barebones example taken from a much more complex script and I’m trying to figure out why the result is what it is. In each iteration all elements that have already been added to $Results have their values updated to the most recent value. I’ve tested forcing everything to $Script: or $Global: scope and get the same results.

The only solution I’ve found is the following, which moves the $Result declaration into the loop.

    $SampleValues = 1..5
    $Results = @()

$SampleValues | %{
    $Result = "" | Select ID
    $Result.ID = $_
    $Results += $Result
    }

This works (you get 1,2,3,4,5 as your results). It looks like $Results is just holding multiple references to a singular $Result object but why does moving this into the loop fix the problem? In this example $Result is a string so perhaps it is creating a new object each iteration but even when I forced $Result to be an integer (which shouldn’t recreate a new object since an integer isn’t immutable like a string) it still fixed the problem and I got the result I expected.

If anybody has any insight into exactly why this fixes the problem I’ve be very curious. There are plenty of alternatives for me to implement but not understanding specifically why this works this way is bugging me.

  • 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-22T23:20:01+00:00Added an answer on May 22, 2026 at 11:20 pm

    It fixes the problem by moving into the loop because then you are then creating a new $Result object each time rather than changing a value on the same one (referenced 5 times in the array).

    It doesn’t have anything to do with whether you use "" | Select ID or 123 | Select ID because that just becomes a sort of property on the PSCustomObject, which is still a reference type rather than a value type.

    Remember, Powershell is all .NET on the inside. Here is some C# that’s analogous to what Powershell is doing in your first example that resulted in all 5s (hopefully you know C#):

    var SampleValues = new []{1,2,3,4,5};
    var Result = new CustomObject(){ ID = "" };
    var Results = new List<Object>();
    
    foreach (var _ in SampleValues) {
        Result.ID = _;
        Results.Add(Result);
    }
    

    Hopefully you can see how moving var Result = new CustomObject(){ ID = "" } inside the foreach loop would make it work better, and the same concept holds true in Powershell.

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

Sidebar

Related Questions

Forgive me if the title is not lucid, but I could not better describe
Forgive me for asking what some might think are stupid questions. I am trying
I've never asked a question here before so please forgive my question if its
Im new to MVC, EF4 and Linq, so forgive my ignorance If im using
I'm pretty new to Python, so I'll hope you forgive me for such amateurish
This is my first attempt at using JSCookMenu and its homepage ( http://jscook.yuanheng.org/JSCookMenu/ )
I'm looking for help identifying this design pattern and learning the typical vocabulary it
Forgive me for asking such a simple question, I'm new to both HTML and
My script is supposed to find all related posts from the current posts categories
I asked something similar a few days ago, here is my issue. My professor

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.