title: Foreach -parallel object
body: <p>Recently we started working on scripts that take a very long time to complete. So we dug into PowerShell workflows. After reading some documentation I understand the basics. However, I can't seem to find a way to create a <code>[PSCustomObject]</code> for each individual item within a <code>foreach -parallel</code> statement.</p>
  
  <p><strong>Some code to explain:</strong></p>
  
  <pre><code>Workflow Test-Fruit {
  
      foreach -parallel ($I in (0..1)) {
  
          # Create a custom hashtable for this specific object
          $Result = [Ordered]@{
              Name  = $I
              Taste = 'Good'
              Price = 'Cheap'
          }
  
          Parallel {
              Sequence {
                  # Add a custom entry to the hashtable
                  $Result += @{'Color' = 'Green'}
              }
  
              Sequence {
                  # Add a custom entry to the hashtable
                  $Result += @{'Fruit' = 'Kiwi'}
              }
          }
  
          # Generate a PSCustomObject to work with later on
          [PSCustomObject]$Result
      }
  }
  
  Test-Fruit
  </code></pre>
  
  <p>The part where it goes wrong is in adding a value to the <code>$Result</code> hashtable from within the <code>Sequence</code> block. Even when trying the following, it still fails:</p>
  
  <pre><code>$WORKFLOW:Result += @{'Fruit' = 'Kiwi'}
  </code></pre>