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

  • SEARCH
  • Home
  • 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 8199489
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T06:10:44+00:00 2026-06-07T06:10:44+00:00

First post, I’ve been researching this all day and I’m at my wits end!

  • 0

First post, I’ve been researching this all day and I’m at my wits end! I have an array called $Global:CBCGroups that I want to contain the value of an ever-changing variable called $Global:CBCTempHolder. Whenever $Global:CBCTempHolder is changed, I want it to add it’s contents to the array $Global:CBCGroups using += (ie:

$Global:CBCGroups += $Global:CBCTempHolder

The columns in CBCGroups are CBCGroup, and Element. The same is present in $Global:CBCTempHolder, but the value changes in every “For” statement. Here is the code:

$Global:CBCGroups = @()


for ($z = 0; $z -lt $Global:UniqueCCData.count; $z +=1)
{
If ($Global:UniqueCCData[$z]."AD Group".Contains("CBC_CBC_") -eq $True)
{
        $Global:CBCTempHolder = @(0) * 1 | select CBCGroup,Element
        $Global:CBCTempHolder.CBCGroup = $Global:UniqueCCData[$z]."AD Group"
        $Global:CBCTempHolder.Element = $z
        $Global:CBCGroups += $Global:CBCTempHolder
        $Global:UniqueCCData[$z]."AD Group" = $Global:UniqueCCData[$z]."AD Group".Replace("CBC_CBC_","CBC.CBC")

    }
}

So, basically what is happening is whenever I add a new element to $Global:CBCGroups via:

$Global:CBCGroups += $Global:CBCTempHolder

I am noticing that whenever I change the value of $Global:CBCTempHolder and then manually change the values of $Global:CBCTempHolder.CBCGroup to anything, add say 5 elements in a row using the assign by addition operator, and then change the value of $Global:CBCTempHolder again manually, it changes the last 5 elements in $Global:CBCGroups to the same value! I need each element in $Global:CBCGroups to maintain it’s value from the time when it was added to the array.

I realize $Global:CBCTempHolder is not a true array, but it contains the two named elements that I need to have added to a new list. How can I achieve this?

  • 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-06-07T06:10:45+00:00Added an answer on June 7, 2026 at 6:10 am

    Here’s code that exhibits the issue:

    $UniqueCCData = @()
    $UniqueCCData += new-object psobject -property @{ "AD Group" = "Domain Users" }
    $UniqueCCData += new-object psobject -property @{ "AD Group" = "CBC_CBC_Enterprise Admins" }
    $UniqueCCData += new-object psobject -property @{ "AD Group" = "CBC_CBC_Help Desk" }
    $UniqueCCData += new-object psobject -property @{ "AD Group" = "CBC_CBC_Marketing" }
    $UniqueCCData += new-object psobject -property @{ "AD Group" = "CBC_CBC_Sales" }
    $UniqueCCData += new-object psobject -property @{ "AD Group" = "Domain Admins" }
    $CBCGroups = @()
    for ($z = 0; $z -lt $UniqueCCData.count; $z += 1)
    {
        if ($UniqueCCData[$z]."AD Group".Contains("CBC_CBC_") -eq $True)
        {
            $CBCTempHolder = @(0) * 1 | select CBCGroup,Element
            $CBCTempHolder.CBCGroup = $UniqueCCData[$z]."AD Group"
            $CBCTempHolder.Element = $z
            $CBCGroups += $CBCTempHolder
            $UniqueCCData[$z]."AD Group" = $UniqueCCData[$z]."AD Group".Replace("CBC_CBC_","CBC.CBC")
        }
    }
    $CBCTempHolder.CBCGroup = "CBC_CBC_VIPs"
    $CBCTempHolder.Element = 5
    $CBCGroups += $CBCTempHolder
    $CBCGroups
    

    Here’s the output:

    CBCGroup                    Element
    --------                    -------
    CBC_CBC_Enterprise Admins         1
    CBC_CBC_Help Desk                 2
    CBC_CBC_Marketing                 3
    CBC_CBC_VIPs                      5
    CBC_CBC_VIPs                      5
    

    The problem is that a reference to the object is assigned. So when the object changes, any objects that are referencing it also appear to change. The way to work around this to assign unique objects to each index of CBCGroups. Here’s how I’d solve it:

    function CBC_clone($group, $i)
    {
        $temp = @(0) * 1 | select CBCGroup,Element
        $temp.CBCGroup = $group.Replace("CBC_CBC_","CBC.CBC")
        $temp.Element = $i
        return $temp
    }
    $UniqueCCData = @()
    $UniqueCCData += new-object psobject -property @{ "AD Group" = "Domain Users" }
    $UniqueCCData += new-object psobject -property @{ "AD Group" = "CBC_CBC_Enterprise Admins" }
    $UniqueCCData += new-object psobject -property @{ "AD Group" = "CBC_CBC_Help Desk" }
    $UniqueCCData += new-object psobject -property @{ "AD Group" = "CBC_CBC_Marketing" }
    $UniqueCCData += new-object psobject -property @{ "AD Group" = "CBC_CBC_Sales" }
    $UniqueCCData += new-object psobject -property @{ "AD Group" = "Domain Admins" }
    $CBCGroups = @()
    for ($z = 0; $z -lt $UniqueCCData.count; $z += 1)
    {
        if ($UniqueCCData[$z]."AD Group".Contains("CBC_CBC_") -eq $True)
        {
            $CBCGroups += (CBC_clone $UniqueCCData[$z]."AD Group" $z)
        }
    }
    $CBCGroups += (CBC_clone "CBC_CBC_VIPs" 5)
    $CBCGroups
    

    This produces the desired output:

    CBCGroup                 Element
    --------                 -------
    CBC.CBCEnterprise Admins       1
    CBC.CBCHelp Desk               2
    CBC.CBCMarketing               3
    CBC.CBCSales                   4
    CBC.CBCVIPs                    5
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is my first post and I my first experience with jquery. I have
first post here=) I've been looking for this answer and haven't been able to
This is my first post here so if I've been mistaken for my post
this is my first post. I have a huge problem which make me headaches.
This is my first post on StackOverflow! I have a background service running and
First post time, I've been playing around with MVC abit... I have a view
First post on stackoverflow, apologies beforehand for anything noob-like. I have been receiving an
first post hopefully you can all help me. Basically I have registered an ajax
First post, but I've been lurking around this site for a long while now,
My first post here :) I have a summer job doing a bit of

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.