title: Powershell - Fastest way to create a very large array of hashtables
body: <p>I am trying to create a rather large array of hashtables, with much of the data either fully randomized or randomly picked from a list.</p>
  
  <p>Here is my <strong>Current Code</strong></p>
  
  <pre class="lang-bsh prettyprint-override"><code>    $ArrayData = @()
      $ArrayDataRows = 150000
  
      foreach ($i in 1..$ArrayDataRows) {
  
          $thisobject = [PSCustomObject] @{
              Number = $i
              Place = Get-Random -InputObject NJ, UT, NY, MI, PA, FL, AL, NM, CA, OK, TX, CO, AZ
              Color = Get-Random -InputObject red, yellow, blue, purple, green, white, black
              Zone = (Get-Random -InputObject $([char[]](65..90)) -Count 10) -join ""
              Group = Get-Random -InputObject @(1..20)
          }
  
  
          $ArrayData += $thisobject 
       }
  </code></pre>
  
  <p>What I notice though, is that it seems to not be efficient. It takes 25 mins in total to finish for 150k rows.</p>
  
  <p>I had some additional code not posted here which measured how long it took each instance and estimated the average from it to its predecessors. Initially, it would give me an estimate for 450 secs for the total and 0.002 as an Average per instance for the first 3k items but later it just kept slowly crawling up to 0.016 or 8 times slower as an average.</p>
  
  <p>How can I optimize and/or make this more efficient while achieving the same data as a result?</p>