title: Change nested object values in a powershell object
body: <p>I have the following object, and I would like to change a nested value in the object without changing all the other nested objects. The object I would like to change is in a variable.</p>
  <pre class="lang-bsh prettyprint-override"><code>$object.Tophashtable.MidHashtable1.array1[0].linenumber = 1
                                               Value      = Bottom-array1-1
                                     array1[1].linenumber = 2
                                               Value      = Bottom-array1-2
                                     array1[2].linenumber = 3
                                               Value      = Bottom-array1-3
                                     array2[0].linenumber = 1
                                               Value      = Bottom-array2-1
                                     array2[1].linenumber = 2
                                               Value      = Bottom-array2-2
                       MidHashtable2.array3[0].linenumber = 1
                                               Value      = Bottom-array3-1
                                     array3[1].linenumber = 2
                                               Value      = Bottom-array3-2
                                     array3[2].linenumber = 3
                                               Value      = Bottom-array3-3
                                     array4[0].linenumber = 1
                                               Value      = Bottom-array4-1
                                     array4[1].linenumber = 2
                                               Value      = Bottom-array4-2
                       MidHashtable3.array5[0].linenumber = 1
                                               Value      = Bottom-array5-1
  </code></pre>
  <p>I would like to do the following:</p>
  <pre><code>  $newobject = @{    
      linenumber = &quot;1&quot;
      value = &quot;newobject-1&quot;}
    $object.$change = $newobject
  </code></pre>
  <p>I'm able to read the content of the subobject with the following code:</p>
  <pre><code>iex &quot;`$object.$change&quot;
  </code></pre>
  <p>But I can't find a way to set/change the subobject.</p>
  <p>code example:</p>
  <pre><code>$object = @{
      tophashtable = @{
          MidHashtable1 = @{
              array1 = @(
                  @{
                      linenumber = &quot;1&quot;
                      value = &quot;Bottum-array1-1&quot;
                  },
                  @{
                      linenumber = &quot;2&quot;
                      value = &quot;Bottum-array1-2&quot;
                  },
                  @{
                      linenumber = &quot;3&quot;
                      value = &quot;Bottum-array1-3&quot;
                  },
                  @{
                      linenumber = &quot;4&quot;
                      value = &quot;Bottum-array1-4&quot;
                  }
              )
              array2 = @(
                  @{
                      linenumber = &quot;1&quot;
                      value = &quot;Bottum-array2-1&quot;
                  },
                  @{
                      linenumber = &quot;2&quot;
                      value = &quot;Bottum-array2-2&quot;
                  }
              )
          }
          MidHashtable2 = @{
              array3 = @(
                  @{
                      linenumber = &quot;1&quot;
                      value = &quot;Bottum-array3-1&quot;
                  },
                  @{
                      linenumber = &quot;2&quot;
                      value = &quot;Bottum-array3-2&quot;
                  },
                  @{
                      linenumber = &quot;3&quot;
                      value = &quot;Bottum-array3-3&quot;
                  },
                  @{
                      linenumber = &quot;4&quot;
                      value = &quot;Bottum-array3-4&quot;
                  }
              )
              array4 = @(
                  @{
                      linenumber = &quot;1&quot;
                      value = &quot;Bottum-array4-1&quot;
                  },
                  @{
                      linenumber = &quot;2&quot;
                      value = &quot;Bottum-array4-2&quot;
                  }
              )
          }
          MidHashtable3 = @{
              array5 = @(
                  @{
                      linenumber = &quot;1&quot;
                      value = &quot;Bottum-array5-1&quot;
                  },
                  @{
                      linenumber = &quot;2&quot;
                      value = &quot;Bottum-array5-2&quot;
                  },
                  @{
                      linenumber = &quot;3&quot;
                      value = &quot;Bottum-array5-3&quot;
                  }
              )
          }
      }
  }
  
  $newobject = @(
      @{
          linenumber = &quot;1&quot;
          value = &quot;newobject-1&quot;
      },
      @{    
          linenumber = &quot;2&quot;
          value = &quot;newobject-2&quot;
      }
  )
  
  $query = &quot;tophashtable.MidHashtable1.array2&quot;
  
  # This does work to read the content of the subobject true a variable
  Invoke-Expression &quot;`$object.$query&quot;
  
  # This does work to set the content of the subobject to the newobject
  $object.tophashtable.MidHashtable1.array2 = $newobject
  
  # I would like to set the content of the subobject by the value of the query variable.
  # The value of the query variable can be of different length as wel. 
  # Sometime's i want to change the array's but sometime's i would like to change the content of the midhashtable's completely so $query would only be &quot;tophashtable.MidHashtable1&quot;
  </code></pre>