1
00:00:01,140 --> 00:00:03,980
Next, let's turn our attention to hashtables.

2
00:00:03,980 --> 00:00:07,390
Now, you may sometimes see hashtables written as two words.

3
00:00:07,390 --> 00:00:11,700
There is an actual .NET type that is hashtable as a single word,

4
00:00:11,700 --> 00:00:14,980
so I'm just going to be using the single word,

5
00:00:14,980 --> 00:00:15,730
hashtable.

6
00:00:15,730 --> 00:00:21,740
A hashtable, again, is another type of data structure, key value pair.

7
00:00:21,740 --> 00:00:25,020
Sometimes this is referred to as like a phone book where a phone book

8
00:00:25,020 --> 00:00:28,050
has the name of someone and their phone number.

9
00:00:28,050 --> 00:00:30,040
If you're a VB script kind of person,

10
00:00:30,040 --> 00:00:32,700
we had something called a dictionary object,

11
00:00:32,700 --> 00:00:35,340
which is the same idea as a hashtable.

12
00:00:35,340 --> 00:00:38,150
I have a key and some corresponding value.

13
00:00:38,150 --> 00:00:40,510
In the hashtable, the keys are unique,

14
00:00:40,510 --> 00:00:46,440
the values can be whatever they need to be, and here is how we work with them.

15
00:00:46,440 --> 00:00:50,540
In PowerShell, you'll often use hashtables to store data.

16
00:00:50,540 --> 00:00:53,410
Hashtables are very flexible, they can also be used to

17
00:00:53,410 --> 00:00:55,450
create custom properties and values.

18
00:00:55,450 --> 00:00:57,790
You can create something that doesn't exist and you

19
00:00:57,790 --> 00:01:01,040
can use a hashtable to do that.

20
00:01:01,040 --> 00:01:04,090
Oftentimes, we can use hashtables as a lookup, remember,

21
00:01:04,090 --> 00:01:05,640
it's a name value pair,

22
00:01:05,640 --> 00:01:11,010
so you can create a hashtable of keys and then lookup some corresponding value.

23
00:01:11,010 --> 00:01:13,400
This is something that might be more useful when you get to

24
00:01:13,400 --> 00:01:18,160
partial scripting. And we can also use PowerShell hashtables

25
00:01:18,160 --> 00:01:21,160
to simplify running command, this is splatting, and again,

26
00:01:21,160 --> 00:01:24,940
I'll get to this in a little bit.

27
00:01:24,940 --> 00:01:26,840
So let's create a hashtable.

28
00:01:26,840 --> 00:01:31,310
Hashtables are very simple to create. The hashtable syntax is

29
00:01:31,310 --> 00:01:35,240
the @ symbol and then a set of curly braces.

30
00:01:35,240 --> 00:01:36,590
Inside the curly braces,

31
00:01:36,590 --> 00:01:42,120
we define our key value pair separated by semicolons if

32
00:01:42,120 --> 00:01:44,230
it's all on the same line, otherwise,

33
00:01:44,230 --> 00:01:47,050
if you just hit Enter after the key value pair,

34
00:01:47,050 --> 00:01:50,640
PowerShell knows okay, whatever is next is another set.

35
00:01:50,640 --> 00:01:56,780
This hashtable $h has two key value pairs, one called name,

36
00:01:56,780 --> 00:02:00,770
which has a value of Jeff, and the other key pair has a key

37
00:02:00,770 --> 00:02:04,640
of Count and the value of 3.

38
00:02:04,640 --> 00:02:09,630
So again, we have the @ symbol Key = Value. Technically yes,

39
00:02:09,630 --> 00:02:13,590
you could have a hashtable of just a single key value

40
00:02:13,590 --> 00:02:17,080
pair, but more than likely, you will be creating multiple

41
00:02:17,080 --> 00:02:20,240
entries inside your hashtable.

42
00:02:20,240 --> 00:02:23,180
They're separated with a semicolon if you're on the same line.

43
00:02:23,180 --> 00:02:26,020
Otherwise, if you just hit Enter, then PowerShell, as I said,

44
00:02:26,020 --> 00:02:28,440
knows what you are doing.

45
00:02:28,440 --> 00:02:33,780
So the hashtable I created is saved to $h. If I type $h, I can see

46
00:02:33,780 --> 00:02:37,760
the hashtable, and in the output, you can see name and value. Name

47
00:02:37,760 --> 00:02:42,360
is the key and then value obviously is the value. So I've got name

48
00:02:42,360 --> 00:02:46,940
was Jeff and count of 3.

49
00:02:46,940 --> 00:02:50,440
Where this gets fun is that you can reference a hashtable

50
00:02:50,440 --> 00:02:55,650
and get a key value just by referencing using the

51
00:02:55,650 --> 00:02:58,190
object.notation like we've done earlier.

52
00:02:58,190 --> 00:03:01,580
So I can do $h.name and that tells PowerShell hey,

53
00:03:01,580 --> 00:03:05,740
show me the name key, show me the value that you have associated with that,

54
00:03:05,740 --> 00:03:09,740
and so I get a value of Jeff written to the pipeline.

55
00:03:09,740 --> 00:03:12,960
So even though I've created a hashtable, it's still just another

56
00:03:12,960 --> 00:03:22,000
type of object and I can use the object.notation in order to access different elements in that hashtable.

