# Arrays that do not behave in a script

**URL:** <https://forums.powershell.org/t/arrays-that-do-not-behave-in-a-script/4981>\
**Category:** PowerShell Help\
**Created:** [October 6, 2015, 7:07pm UTC](https://forums.powershell.org/t/arrays-that-do-not-behave-in-a-script/4981 "2015-10-06T19:07:45Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![mark-brown-2](https://avatars.discourse-cdn.com/v4/letter/m/f07891/32.png) [@mark-brown-2](https://forums.powershell.org/u/mark-brown-2)\
**Post date:** [October 6, 2015, 7:07pm UTC](https://forums.powershell.org/t/arrays-that-do-not-behave-in-a-script/4981/1 "2015-10-06T19:07:45Z")

</div>

I am trying to create a very simple array of values. From a PS C:&gt; everything works as expected. But as soon as I use a script, the arrays start to misbehave. Instead of creating new array of elements, I am just getting a single concatenated string. What as I doing wrong?

cls

$a = “loader1x”  
$a  
$a += “loader2x”  
$a  
$a += “loader3x”  
$a  
$a += “loader4x”  
$a  
$a.length  
$a[1]  
$a[3]  
foreach($b in $a)  
{  
$b  
$b.length

}

Output:

loader1x  
loader1xloader2x  
loader1xloader2xloader3x  
loader1xloader2xloader3xloader4x  
32  
o  
d  
loader1xloader2xloader3xloader4x  
32

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex019/uploads/powershell/original/1X/385e4f9fe133561ad30a814262fe0e0ae6bc3ef0.png) [@system](https://forums.powershell.org/u/system)\
**Post date:** [October 6, 2015, 8:24pm UTC](https://forums.powershell.org/t/arrays-that-do-not-behave-in-a-script/4981/2 "2015-10-06T20:24:41Z")

</div>

The += operator behavior depends on what’s on the left side. Your $a variable is originally declared as a string, not an array, so you get string concat behavior. Declaring it as an array will change that behavior:

```
$a = @()

$a += 'A string'
$a += 'Another string'
# etc
```

---

<div class="post-metadata">

**Author:** ![daniel-krebs](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/daniel-krebs/32/484_2.png) [@daniel-krebs](https://forums.powershell.org/u/daniel-krebs)\
**Post date:** [October 6, 2015, 8:26pm UTC](https://forums.powershell.org/t/arrays-that-do-not-behave-in-a-script/4981/3 "2015-10-06T20:26:05Z")

</div>

You will need to tell the PowerShell engine that you want an array object with @() for example otherwise $a = “loader1x” will just be a string object.

Try below:

```
cls

$a = @("loader1x")
$a
$a += "loader2x"
$a
$a += "loader3x"
$a
$a += "loader4x"
$a
$a.length
$a[1]
$a[3]
foreach($b in $a)
{
    $b
    $b.length
}
```

---

<div class="post-metadata">

**Author:** ![mark-brown-2](https://avatars.discourse-cdn.com/v4/letter/m/f07891/32.png) [@mark-brown-2](https://forums.powershell.org/u/mark-brown-2)\
**Post date:** [October 6, 2015, 11:25pm UTC](https://forums.powershell.org/t/arrays-that-do-not-behave-in-a-script/4981/4 "2015-10-06T23:25:20Z")

</div>

Thanks Guys,  
Dave, your solution doesn’t seem to work for me. However, Daniel’s does. I can get back to work now!

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:44pm UTC](https://forums.powershell.org/t/arrays-that-do-not-behave-in-a-script/4981/5 "2024-05-16T20:44:41Z")

</div>


