Copying PowerShell Hashtables (The Right Way!)
4K views
Jun 11, 2024
Avoid frustration and learn how to copy a hashtable (the right way)! ? Get the Code Here! ? https://github.com/JeffBrownTech/jeffbrowntech_youtube/tree/main/2024-06-10-CopyingHashTables ? Read More About This ? https://jeffbrown.tech/copying-powershell-hashtables-the-right-way/ ⏰ Chapters ⏰ 00:00 Introduction 00:17 Copying Strings Example 01:13 Copying Hashtables 04:01 Copying Hashtables (The Right Way) 07:00 Summary Social Links ? LinkedIn: https://www.linkedin.com/jeffbrowntech ? X: https://www.twitter.com/jeffwbrown ? Threads: https://www.threads.net/@jeffbrowntech ? Mastodon: https://mastodon.social/@JeffBrownTech
View Video Transcript
0:00
What's up everyone? Today I want to talk about how you can copy hash tables the right way
0:04
This is an issue I ran into once and it took a little bit of research and digging to figure out why it was like this
0:10
This was before chat GPT existed. And I wanted to share my learnings with you here. Let's go ahead and get started
0:17
First I want to show just an example of copying something and how you might expect something to work
0:23
Let's create a couple of strings here. We have string1 and it just says this is my string
0:28
Then we're going to create string2 and set it equal to string1
0:33
And we're going to just output it to the screen and as expected these two strings should output the same thing
0:41
Now let's take string2 and just append something to it here and then output both string1 and string2 again
0:48
And we should see two different values now. And that is expected. We have this is my string and this is my string.appending to string2
0:58
So I made string1, I set string2 to the same value as string1, I modified string2 and they now have different values here
1:07
That's all very intuitive there I think so let's go try to do the same thing with a hash table
1:13
I have person1 here variable. It has a couple of properties name and profession
1:20
Let's go ahead and create person1. And let's just go through I'm going to output it out and we see okay perfect we have our two properties here
1:29
So let's do the exact same thing here. Let's do person2 equals person1
1:36
And again we will output both of those. It doesn't separate them there it's just outputting since they have the same name values they're both hash tables but we do see
1:44
name and profession twice here with the same values just showing that they are the same right now
1:50
Alright now that we have our person1 hash table and person2 hash table they have the same values in them right now
1:57
Let's go ahead and add something new to person2 so it is a little bit different than person1
2:03
We'll go ahead and add that. And let's output person1. Uh oh so it also has state in it now suddenly
2:11
And so does person2 even though that's not what we initially set person1 only had
2:17
name and profession in it it didn't have state. We added state to person2 but it somehow showed up on person1 here
2:25
Why is this happening? Let's go ahead and scroll down to the little explanation here
2:30
Hash tables are objects and our variables person1 and person2 are actually only referencing that object's place in memory
2:40
They're not two separate variables. Both variables are pointing to that data structure inside of memory inside the same location
2:50
And as I said here each variable is actually pointing to that same hash table not two separate ones
2:56
And that's not the same thing we see with strings. When you create two strings like that and copy them it actually creates a new place in memory and its own value there
3:06
We do have a .NET method here where we can reference to see if they're both looking at the same place in memory
3:12
Let's take a look and see what that looks like. We'll go back to our string example here we have string1 and string2
3:17
If we were to run this it comes back false because they are not referencing the same place in memory
3:24
But if we go back and look at person1 and person2 that does come back as true
3:29
because again they are pointing to the same place inside of memory there
3:35
I know this might be a little weird concept if you've only done things inside of PowerShell
3:41
I have taken some C++ classes before and looking at pointers and things like that so I do understand it just a little bit better
3:47
But just think about whenever you create a variable that variable gets a little spot in memory to store that value
3:55
And in this case person1 and person2 are both pointing to that same spot inside of memory
4:01
That brings us back to how can we actually copy a hash table so it has its own place in memory
4:08
and you can modify it and add or move or set different properties for it
4:13
The first method we have is to clone the hash table. Here on line 42 we're going to create person3 and we are going to reference person1 and call the clone method
4:27
So go ahead and run that. And in this case for person3 we're going to add a new key value pair of city
4:34
Go ahead and add that. And let's go ahead and look at the output of person3
4:39
Now don't forget when we added state to person2 it also did it for person1
4:44
That's why person3 also has state or our fourth property here. In this case when we look at person1 it only has name, profession, and state
4:54
Or person3 has city, name, profession, and state. It has our four properties there
4:59
This means we actually were able to make a new copy of that hash table inside of memory for person3
5:06
and we are able to modify its values independently of person1. In fact let's take this here and let's go back to person1 and person3
5:19
We'll update there and see if that comes back true or false
5:23
And as expected it comes back false because they are not referencing the same place inside of memory
5:28
Person3 is its own spot or object. Cloning is our first method to actually create a new hash table
5:35
You can also do it with a foreach. I actually haven't tried this so we'll see if it works
5:40
On line 50 we have a new person4. We're setting it to an empty hash table
5:46
And then we're going to run through a foreach. We can actually run through each person1's keys that it has existing there
5:54
Actually let me highlight this and show you. Our keys right now for person1 are name, profession, and state
6:01
So we're going to iterate through each of those. We're then going to set it on person4 the value as it matches in person1
6:09
So we'll go ahead and highlight our foreach here. Okay, I see what I did here. I got ahead of myself a little bit
6:17
Let's go ahead and bring person4 into our session here. I didn't actually define that beforehand
6:23
Clear out the screen. Go ahead and highlight this now. And run through it. Okay, no errors. Perfect
6:30
Told you I didn't do it beforehand. Now let's go into person4. We'll add county
6:36
And then let's go ahead and output person4. It is name, profession, state, county
6:43
And let's go look at person1. They just have name, profession, and state
6:49
So they do not reference the same place. And just to be thorough, let's go back here
6:56
We'll compare person4. And that comes back as false. So hopefully this made sense
7:02
Copying hash tables is not the same thing as copying integers or strings
7:07
You actually have to make a full other copy of it. There's probably a good reason why it's like this
7:13
I didn't dig into why hash tables are different. But just in case you need to copy a hash table and you want to do it the right way
7:20
and not try to figure out what in the heck is going on. I don't remember what kind of script I was writing when I did this
7:25
but I kept trying to figure out why is my original object being modified
7:29
when I'm only modifying the second hash table. Took a little bit of digging, and this is what I came up with at the time
7:35
Anyway, hopefully this helps you out. Thank you for watching, and we'll see you next time
#Computers & Electronics
#Programming
#Scripting Languages
#Technical Reference