1
00:00:01,040 --> 00:00:04,600
Now let me show you a few other tricks here with hash tables.

2
00:00:04,600 --> 00:00:07,270
Let me create an array, $j, and just give it a single

3
00:00:07,270 --> 00:00:10,400
entry of name with the value of foo.

4
00:00:10,400 --> 00:00:10,730
Now,

5
00:00:10,730 --> 00:00:13,680
later on, I decide, you know what, I want to add another

6
00:00:13,680 --> 00:00:16,240
entry called name with the value of bar,

7
00:00:16,240 --> 00:00:19,720
maybe not realizing what I have already done. And PowerShell

8
00:00:19,720 --> 00:00:24,060
will complain because a hash table key has to be unique, and I

9
00:00:24,060 --> 00:00:25,680
already have an entry for name.

10
00:00:25,680 --> 00:00:28,670
One thing you can do is to test if the hash table already

11
00:00:28,670 --> 00:00:32,050
contains that key, and you can do that with a method of the

12
00:00:32,050 --> 00:00:36,040
hash table. So I pipe $j to Get‑Member.

13
00:00:36,040 --> 00:00:39,540
I know there are methods that have contains in the name.

14
00:00:39,540 --> 00:00:41,990
There's Contains, ContainsKey, which basically do the

15
00:00:41,990 --> 00:00:44,530
same thing, and then ContainsValue.

16
00:00:44,530 --> 00:00:47,130
These will give you a boolean result,

17
00:00:47,130 --> 00:00:52,610
basically true or false, if the key exists or if the value already exists in

18
00:00:52,610 --> 00:00:59,870
the hash table. So I can do $j.ContainsKey('name'), and sure enough, it does,

19
00:00:59,870 --> 00:01:02,810
and I'll get the same result if I use Contains.

20
00:01:02,810 --> 00:01:05,410
So here's how we might use this in a more practical

21
00:01:05,410 --> 00:01:09,950
perspective. I'm going to run Get‑Process. And for every

22
00:01:09,950 --> 00:01:13,100
process object that comes through the pipeline,

23
00:01:13,100 --> 00:01:17,790
I'm going to do something. Now before I do something,

24
00:01:17,790 --> 00:01:20,490
I'm going to initialize an empty hash table,

25
00:01:20,490 --> 00:01:22,940
$p.

26
00:01:22,940 --> 00:01:25,160
Now as every object comes through the pipeline,

27
00:01:25,160 --> 00:01:26,610
I'm first going to test, hey,

28
00:01:26,610 --> 00:01:33,140
does $p already contain an entry using the name of the process as the key?

29
00:01:33,140 --> 00:01:34,260
If it does,

30
00:01:34,260 --> 00:01:38,550
then I'm going to update the value with the working

31
00:01:38,550 --> 00:01:40,470
set value of the current process.

32
00:01:40,470 --> 00:01:45,440
I'm using the += sign to increment and update the value.

33
00:01:45,440 --> 00:01:51,930
So the hash table will have a key for the process name, and the value will be

34
00:01:51,930 --> 00:01:59,240
the sum of the working set value of the process object.

35
00:01:59,240 --> 00:02:04,460
Otherwise, if $p does not contain the name of the process,

36
00:02:04,460 --> 00:02:08,440
then I will just go ahead and add it to the hash table and then add

37
00:02:08,440 --> 00:02:13,440
the current working set to my starting point.

38
00:02:13,440 --> 00:02:17,670
So when I run this, $p now has this information. I have the

39
00:02:17,670 --> 00:02:20,920
process name and then the working set size.

40
00:02:20,920 --> 00:02:24,830
So using that contains method allowed me to nicely and

41
00:02:24,830 --> 00:02:29,940
easily update the hash table as I needed to.

42
00:02:29,940 --> 00:02:33,540
Now here's, though, another place where something may trip you up.

43
00:02:33,540 --> 00:02:35,350
I then decide, you know what, I want to sort that

44
00:02:35,350 --> 00:02:38,540
output on the name of the process.

45
00:02:38,540 --> 00:02:41,540
So we try something like that, and we go, hey, that did not work.

46
00:02:41,540 --> 00:02:45,440
This is where things like Get‑Member become very important.

47
00:02:45,440 --> 00:02:50,890
If I take $p and pipe it to Get‑Member, you can see that PowerShell is getting

48
00:02:50,890 --> 00:02:56,160
a System.Collection.Hashtable object from $p. It's not a process object, and

49
00:02:56,160 --> 00:03:01,960
it's not the entry for the process object, so it doesn't know about that name

50
00:03:01,960 --> 00:03:07,300
property. We can see it's not listed there. Instead, what you need to do is do

51
00:03:07,300 --> 00:03:10,940
something called GetEnumerator.

52
00:03:10,940 --> 00:03:12,840
So I'm going to take GetEnumerator.

53
00:03:12,840 --> 00:03:15,140
This is a method on the hash table.

54
00:03:15,140 --> 00:03:18,130
If I pipe this to Get‑Member to show you, you can see

55
00:03:18,130 --> 00:03:20,040
now this is a different object type,

56
00:03:20,040 --> 00:03:26,040
a System.Collections.DictionaryEntry. And look at the properties, Key and Value.

57
00:03:26,040 --> 00:03:29,040
And I believe starting in PowerShell 7, they added an

58
00:03:29,040 --> 00:03:31,910
AliasProperty of Name for the Key.

59
00:03:31,910 --> 00:03:37,740
Now what you need to do is this, so I take $p GetEnumerator. I can

60
00:03:37,740 --> 00:03:42,770
still use my Sort‑Object, but the property that I want is Name or Key

61
00:03:42,770 --> 00:03:44,550
if I want to use the actual value. Here,

62
00:03:44,550 --> 00:03:47,940
I'm just using the AliasProperty.

63
00:03:47,940 --> 00:03:52,100
Now I get the results that I expect, and it sorts the way that I expect

64
00:03:52,100 --> 00:03:56,610
because I'm using the proper property name now that I know what type of

65
00:03:56,610 --> 00:04:00,740
object PowerShell is getting in the pipeline.

66
00:04:00,740 --> 00:04:05,020
I can do $p GetEnumerator and filter and say, hey, get me all the

67
00:04:05,020 --> 00:04:09,730
ones where the value is greater than 250 megabytes. And see, I've got

68
00:04:09,730 --> 00:04:14,590
one entry listed there. So knowing how to use GetEnumerator is

69
00:04:14,590 --> 00:04:18,290
important. If you are using a hash table and you're not getting the

70
00:04:18,290 --> 00:04:25,000
results you are expecting, it may be because you need to use this GetEnumerator method.

