Update a Value in an Array When Using Match Operator

This topic contains 1 reply, has 1 voice, and was last updated by  AK 1 year, 1 month ago.

  • Author
  • #68764


    Hello Everyone,

    I have this array:

    $hashtable = @{'NAME' = "Name1";
    'NODE' = "Node1";
     $node = New-Object -TypeName psobject -Property $hashtable
     $nodess +=  $node
    # add several nodes (Node2, Node3, etc.) to this array then use -match to find a node
    if ($node.Name -match $mynodename)
                $node.NODE = "NewNode"

    I'd like to use -match to find and update a record per example above. Is there a good way to do this without doing a "for each" and finding an index?
    I get this error when trying to update a value:
    The property 'NODE' cannot be found on this object. Verify that the property exists and can be set.

  • #68803


    Alright, ended up using

     $index = $node.Name.Indexof($mynodename)
     $node[$index].NODE = "NewNode"

You must be logged in to reply to this topic.