0

I want to replace a template of data $Arraytest with actual data from an XML file Arraytest2.

So I to replace the $Arraytest.Values with those from $Arraytest2.Values and save them for further process.

$Arraytest = @{
    TLC     = 'TLC'
    Crew3LC = 'Crew3LC'
    MyText  = 'MyText'
}

$Arraytest2 = @{
    TLC     = 'FWE'
    Crew3LC = 'KMU'
    MyText  = 'Hello'
}

foreach ($Value in $Arraytest) {
    $Value.Values
}
2
  • @JamesC. This will not create a clone, it sets a reference. Changes made to $arraytest will also be present in $arraytest2 Commented Jun 14, 2018 at 11:53
  • Edit fail, I'd deleted .Clone() somehow. Added as an answer instead. Commented Jun 14, 2018 at 12:42

2 Answers 2

1

Your objects are hashtables, not arrays:

$Arraytest | Get-Member

TypeName: System.Collections.Hashtable     

So you can update using the built in hashtable keys:

$Arraytest = @{
    TLC      = 'TLC'
    Crew3LC  = 'Crew3LC'
    MyText   = 'MyText'
}

$Arraytest2 = @{ 
    TLC      = 'FWE'
    Crew3LC  = 'KMU'
    MyText   = 'Hello'
}

foreach($key in $($Arraytest.keys)){
    $ArrayTest[$key] = $ArrayTest2[$key] 
}

$ArrayTest

Name                           Value                                                                                                                                                                                 
----                           -----                                                                                                                                                                                 
Crew3LC                        KMU                                                                                                                                                                                   
TLC                            FWE                                                                                                                                                                                   
MyText                         Hello   
Sign up to request clarification or add additional context in comments.

Comments

0

You can use Clone() for a shallow copy:

$Arraytest = $Arraytest2.Clone()

A shallow copy will be fine for this case, but see Remarks for further info on this.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.