# How does an array treat a variable if it's commented out?

**URL:** https://forums.powershell.org/t/how-does-an-array-treat-a-variable-if-its-commented-out/13845
**Category:** PowerShell Help
**Created:** [February 20, 2020, 3:50pm UTC](https://forums.powershell.org/t/how-does-an-array-treat-a-variable-if-its-commented-out/13845 "2020-02-20T15:50:17Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![markyodo](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/markyodo/32/133_2.png) [@markyodo](https://forums.powershell.org/u/markyodo)
#### Post date: [February 20, 2020, 3:50pm UTC](https://forums.powershell.org/t/how-does-an-array-treat-a-variable-if-its-commented-out/13845/1 "2020-02-20T15:50:17Z")

</div>

I was just testing and learning and need some clarification on how an array treats a variable if it’s commented out higher in the code. Example:

```
$value1 = "1"
#$value2 = "2"
#$value3 = "3"
$array = @($value1,$value2,$value3)
```

I did some testing using $array[0] (and 1,2) and it always displays $value1 which would make sense.  
So are these values ignored?

---

<div class="post-metadata">

### Author: ![grokkit](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/grokkit/32/970_2.png) [@grokkit](https://forums.powershell.org/u/grokkit)
#### Post date: [February 20, 2020, 6:24pm UTC](https://forums.powershell.org/t/how-does-an-array-treat-a-variable-if-its-commented-out/13845/2 "2020-02-20T18:24:00Z")

</div>

It seems that essentially you’re asking if $null values will be added to the array or not. The answer is yes - if you assign an empty or nonexistent variable to an array, then the array element for that position (whatever it should be for that array) will be added to the array with a value of $null. You can see this happen like this:

```
$a = 1
#$b = 2
$c = 3

$array = @( $a, $b, $c )

echo $array
1
3

echo $array.Count
3
```

The count shows that there are 3 elements in the array, even though echoing the contents of the array only shows the values for $a and $c. So, even though $b has not been assigned a value, it was still accounted for when the array was created. When the shell called $b while creating the array, it found that the value of $b was $null and assigned that value to the array element (the shell follows your instructions exactly, even if your instructions are wrong or don’t make sense).

This is true in general - any time that you call a value that does not exist (variable/array/array element/property/etc), the value is $null. This is something that has to be handled carefully, as it can creep into your script due to a typo (e.g. you have a variable $beta but you referenced $bta instead and now your script is using a value of $null). Your script can continue operating with no obvious problem until the $null value causes something else to fail, which may be difficult to track down when troubleshooting.

Here are some more thorough explanations:  
[Powershell: Everything you wanted to know about arrays: $null or empty](https://powershellexplained.com/2018-10-15-Powershell-arrays-Everything-you-wanted-to-know/#null-or-empty)  
[Powershell: Everything you wanted to know about $null](https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_variables?view=powershell-7)

---

<div class="post-metadata">

### Author: ![markyodo](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/markyodo/32/133_2.png) [@markyodo](https://forums.powershell.org/u/markyodo)
#### Post date: [February 21, 2020, 8:24am UTC](https://forums.powershell.org/t/how-does-an-array-treat-a-variable-if-its-commented-out/13845/3 "2020-02-21T08:24:03Z")

</div>

Thank you for the explanation and examples. I have one more question I didn’t find info for in the links provided.  
What if you wanted to test for a value that’s not null? How would this be done?  
Example - does not work, I also tried like and contains:

```
#$a = 1
$b = 2
#$c = 3
$array = @( $a, $b, $c )
if ($array -eq "1")
{
Write-Host "value 1"
}

if ($array -eq "2")
{
Write-Host "value 2"
}
if ($array -eq "3")
{
Write-Host "value 3"
}
```

---

<div class="post-metadata">

### Author: ![rob-simmers](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/rob-simmers/32/1010_2.png) [@rob-simmers](https://forums.powershell.org/u/rob-simmers)
#### Post date: [February 21, 2020, 9:34am UTC](https://forums.powershell.org/t/how-does-an-array-treat-a-variable-if-its-commented-out/13845/4 "2020-02-21T09:34:00Z")

</div>

You can just clean the array to ensure there are no value NULL values:

```
PS C:\Users\rasim> 
#$a = 1
$b = 2
#$c = 3
$array = @( $a, $b, $c )

PS C:\Users\rasim> $array.Count
3

PS C:\Users\rasim> $arraysWithNoNull = $array | Where{$_}

PS C:\Users\rasim> $arraysWithNoNull.Count
1
```

---

<div class="post-metadata">

### Author: ![markyodo](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/markyodo/32/133_2.png) [@markyodo](https://forums.powershell.org/u/markyodo)
#### Post date: [February 21, 2020, 11:27am UTC](https://forums.powershell.org/t/how-does-an-array-treat-a-variable-if-its-commented-out/13845/5 "2020-02-21T11:27:24Z")

</div>

Also testing your original example on my computer does not work the same for me? Is there a version dependency? It’s still seeing the nulls.

```
$a = 1
#$b = 2
$c = 3
$array = @( $a, $b, $c )
echo $array
```

**Produces a result of:**  
1  
2  
3

---

<div class="post-metadata">

### Author: ![grokkit](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/grokkit/32/970_2.png) [@grokkit](https://forums.powershell.org/u/grokkit)
#### Post date: [February 21, 2020, 11:43am UTC](https://forums.powershell.org/t/how-does-an-array-treat-a-variable-if-its-commented-out/13845/6 "2020-02-21T11:43:59Z")

</div>

> [@](#):
>
> It’s still seeing the nulls.

This is not really correct. By commenting out the variable assignment, you are not changing the value of $b. You have not done anything to $b. This is not the same thing as assigning $b a value of $null. If $b contained data before, it still does now. If you want to set $b to a value of $null (remove the value of $b) then you have to do it explicitly:

```
$b = $null
```

Your $array also needs to be cleared if you are using it again.

Even if _you_ know that you want an array or variable to be reset, _the shell_ does not know that unless you tell it.

---

<div class="post-metadata">

### Author: ![markyodo](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/markyodo/32/133_2.png) [@markyodo](https://forums.powershell.org/u/markyodo)
#### Post date: [February 21, 2020, 12:15pm UTC](https://forums.powershell.org/t/how-does-an-array-treat-a-variable-if-its-commented-out/13845/7 "2020-02-21T12:15:46Z")

</div>

Thanks again, this is what I was missing “clearing the array and variables”. When I added this to the script is works as expected.

---

<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:30pm UTC](https://forums.powershell.org/t/how-does-an-array-treat-a-variable-if-its-commented-out/13845/8 "2024-05-16T20:30:25Z")

</div>


