PowerShell array initialization

Eric Ness picture Eric Ness · Oct 22, 2008 · Viewed 211.9k times · Source

What's the best way to initialize an array in PowerShell?

For example, the code

$array = @()
for($i=0; $i -lt 5;$i++)
{
    $array[$i] = $FALSE
}

generates the error

Array assignment failed because index '0' was out of range.
At H:\Software\PowerShell\TestArray.ps1:4 char:10
+         $array[$ <<<< i] = $FALSE

Answer

halr9000 picture halr9000 · Oct 24, 2008

Here's two more ways, both very concise.

$arr1 = @(0) * 20
$arr2 = ,0 * 20