1

Issue

When assigning a (placeholder) hashtable to another variable and then resetting said hashtable, it also resets the value assigned to the variable.

Questions

  1. Why is it happening?
  2. How can I prevent that?
  3. Why does it only seem to be working this way for that specific example, but not if $a is a simple integer?

MWE

$a = @{test=$true}
$b = $a
$b
$a.Clear()
$b

Output

Name                           Value
----                           -----
test                           True

The second call does not output anything as $b is now considered as empty.

Akaizoku
  • 456
  • 5
  • 19
  • 1
    it´s a special hashtable thing, not exactly sure why it happens but you can do this: `$b = $a.clone()` – Paul Aug 29 '18 at 16:32
  • Simple as that, thanks. I encourage you to write a proper answer so that I can accept it and close the thread. Though I would be happy if someone could explain why does that happen with hashtables. – Akaizoku Aug 29 '18 at 16:39

1 Answers1

2

I believe that you need to do something like this:

$b = $a.Clone()

Otherwise, you are referencing the same thing with both $a and $b in regards to hashes.