1
00:00:02,740 --> 00:00:03,180
Obviously,

2
00:00:03,180 --> 00:00:06,660
there is a lot more that we can do and will want to do with hashtables.

3
00:00:06,660 --> 00:00:07,510
So once again,

4
00:00:07,510 --> 00:00:10,250
let's go back into PowerShell and let me demonstrate

5
00:00:10,250 --> 00:00:13,440
how you can work with hashtables.

6
00:00:13,440 --> 00:00:18,160
To create a hashtable, you can either create an empty hashtable like this,

7
00:00:18,160 --> 00:00:24,140
so $ht = @ symbol and a set of curly braces,

8
00:00:24,140 --> 00:00:30,140
or you can create a hashtable and assign values as you create it.

9
00:00:30,140 --> 00:00:33,160
This hashtable has three key value pairs.

10
00:00:33,160 --> 00:00:35,740
The first one is name with the value of jeff,

11
00:00:35,740 --> 00:00:40,040
count, which has a value of 3, and then Sample Entry,

12
00:00:40,040 --> 00:00:42,140
which has a value of This is something.

13
00:00:42,140 --> 00:00:47,090
Notice that sample entry has double quotes around the name of the

14
00:00:47,090 --> 00:00:49,540
key and that's because there is a space in that.

15
00:00:49,540 --> 00:00:52,380
Typically, I would avoid using spaces in my keys,

16
00:00:52,380 --> 00:00:55,360
it just simplifies everything in PowerShell,

17
00:00:55,360 --> 00:00:58,940
but if you need to have it, that's how you would do it.

18
00:00:58,940 --> 00:01:00,870
Because I'm doing this all on one line,

19
00:01:00,870 --> 00:01:07,540
I use a semicolon to separate the key value pairs.

20
00:01:07,540 --> 00:01:10,770
The hashtable is its own special type of object.

21
00:01:10,770 --> 00:01:13,020
You can see there it's a System.Collection.Hashtable,

22
00:01:13,020 --> 00:01:15,520
and if you want to view the hashtable,

23
00:01:15,520 --> 00:01:17,860
just obviously look at the variable and there we go.

24
00:01:17,860 --> 00:01:21,690
So there are my three keys which show up as name and

25
00:01:21,690 --> 00:01:25,140
then the value that goes with each one.

26
00:01:25,140 --> 00:01:28,720
If I want to, I can add an element to the hashtable,

27
00:01:28,720 --> 00:01:35,640
so I'm going to add a key called version with a value of 7.

28
00:01:35,640 --> 00:01:39,740
Now you can see there is my new entry.

29
00:01:39,740 --> 00:01:45,440
If I want to, I can also change values in the hashtable.

30
00:01:45,440 --> 00:01:49,780
So I'm going to take that count property and use the ++ operator which

31
00:01:49,780 --> 00:01:54,940
will take the value of count and increment it by 1,

32
00:01:54,940 --> 00:02:02,640
and sure enough, it is now 4, or I can also assign a new value such as 10.

33
00:02:02,640 --> 00:02:07,290
You can reference elements by their key name.

34
00:02:07,290 --> 00:02:11,240
So if I want to get the name entry from the hashtable,

35
00:02:11,240 --> 00:02:17,240
I can do $ht.Name and I get Jeff as the result.

36
00:02:17,240 --> 00:02:21,130
There are a couple other ways that you can reference items in a hashtable.

37
00:02:21,130 --> 00:02:26,640
You can use this syntax where you put the key name in square brackets,

38
00:02:26,640 --> 00:02:31,240
it gives you then the value,

39
00:02:31,240 --> 00:02:33,630
and I think this approach is easier if you have a

40
00:02:33,630 --> 00:02:38,340
key that has a space in the name.

41
00:02:38,340 --> 00:02:41,320
Another option that you can use is the item method and all you

42
00:02:41,320 --> 00:02:44,080
have to do is specify the name of the key,

43
00:02:44,080 --> 00:02:48,440
in this case, name and I get the result of Jeff.

44
00:02:48,440 --> 00:02:49,350
With hashtables,

45
00:02:49,350 --> 00:02:55,340
you can list all of the keys and you can also list all of the values.

46
00:02:55,340 --> 00:02:57,580
If you need to remove an element from an array,

47
00:02:57,580 --> 00:03:02,040
there is a remove method and just specify the key.

48
00:03:02,040 --> 00:03:04,260
There is nothing really that shows you that it's

49
00:03:04,260 --> 00:03:07,440
gone until you look at it again.

50
00:03:07,440 --> 00:03:11,260
It is possible to completely remove all of the entries

51
00:03:11,260 --> 00:03:14,940
in the array using the clear method.

52
00:03:14,940 --> 00:03:18,790
And now if I do $ht, I can see that there is nothing in it.

53
00:03:18,790 --> 00:03:24,440
The variable still exists, but is an empty hashtable.

54
00:03:24,440 --> 00:03:29,040
You can use hashtables to really store just about anything that you want.

55
00:03:29,040 --> 00:03:32,340
So let's build another hashtable.

56
00:03:32,340 --> 00:03:38,640
I'll create my entry name and now I'll do the count again for 3.

57
00:03:38,640 --> 00:03:41,340
Notice that because I'm not doing this all on the same line,

58
00:03:41,340 --> 00:03:43,740
I don't have to put in the semicolon.

59
00:03:43,740 --> 00:03:45,010
As soon as I hit Enter,

60
00:03:45,010 --> 00:03:48,130
PowerShell knows the next thing that I type is

61
00:03:48,130 --> 00:03:52,210
going to be another key value pair, so I don't have to use semicolons here.

62
00:03:52,210 --> 00:03:57,640
You can if you want, but it's not required when you're doing it this way.

63
00:03:57,640 --> 00:03:59,930
So let's run Get‑Process and get the PowerShell

64
00:03:59,930 --> 00:04:03,640
process and save that to the key ps.

65
00:04:03,640 --> 00:04:07,560
Let's skip all of the services, and this time just to keep it interesting,

66
00:04:07,560 --> 00:04:10,320
I'm going to use Get‑CimInstance to retrieve all

67
00:04:10,320 --> 00:04:16,740
instances of the win32_service class, save that to the key services.

68
00:04:16,740 --> 00:04:20,830
You can also create nested hashtables.

69
00:04:20,830 --> 00:04:23,340
So I'm going to create another hashtable here,

70
00:04:23,340 --> 00:04:26,340
has keys of foo, vendor, and color,

71
00:04:26,340 --> 00:04:31,380
and that will then become a nested hashtable in the hashtable $ht.

72
00:04:31,380 --> 00:04:35,340
There is really no end to how far you can go or need to go.

73
00:04:35,340 --> 00:04:40,620
Put in my closing curly brace and that defines then the hashtable $ht.

74
00:04:40,620 --> 00:04:44,590
It took a moment because it had to go and get all of those services.

75
00:04:44,590 --> 00:04:47,340
So what's in $ht now?

76
00:04:47,340 --> 00:04:51,440
Alright, so there is the name, there is my nested property,

77
00:04:51,440 --> 00:04:55,150
the count services, and the PS key.

78
00:04:55,150 --> 00:04:59,040
Let's look at these items a little more closely.

79
00:04:59,040 --> 00:05:02,160
So I have a hashtable that has a key of services and

80
00:05:02,160 --> 00:05:07,380
the value of services is an array, which means I can use array techniques,

81
00:05:07,380 --> 00:05:12,240
such as getting the first five services in that element,

82
00:05:12,240 --> 00:05:15,040
and there we go.

83
00:05:15,040 --> 00:05:19,940
If I want to get the PS key, which is the PowerShell process, there we go.

84
00:05:19,940 --> 00:05:21,670
Again, this is just another object,

85
00:05:21,670 --> 00:05:24,700
and I can continue to use the object.notation.

86
00:05:24,700 --> 00:05:28,840
So if I want to get the CommandLine property of that object,

87
00:05:28,840 --> 00:05:32,640
sure enough, that's pretty simple to do.

88
00:05:32,640 --> 00:05:35,040
Let's look at the nested hashtable.

89
00:05:35,040 --> 00:05:36,910
So there is my necid hashtable.

90
00:05:36,910 --> 00:05:42,140
Again, I can continue to use the dotted notation.

91
00:05:42,140 --> 00:05:49,640
If I want to, I can change the color entry in that nested hashtable to red,

92
00:05:49,640 --> 00:05:53,340
and now that nested hashtable has an updated value.

93
00:05:53,340 --> 00:05:55,950
There is no limit really to what you can do and how

94
00:05:55,950 --> 00:05:59,140
you might want to use a hashtable.

95
00:05:59,140 --> 00:06:03,740
Now, one thing I want to point out, let's go back and look at $ht,

96
00:06:03,740 --> 00:06:05,800
you can see that it's listed here as Name,

97
00:06:05,800 --> 00:06:08,150
nested, account, services, and ps as the keys,

98
00:06:08,150 --> 00:06:12,040
but that is not the order that I created the entries.

99
00:06:12,040 --> 00:06:14,480
Hashtables are unordered,

100
00:06:14,480 --> 00:06:18,810
meaning there is no guarantee that it will be displayed in the same

101
00:06:18,810 --> 00:06:24,140
order that you created or added the key value pairs.

102
00:06:24,140 --> 00:06:29,940
If you need that, there is a way to do that and we're going to do it like this.

103
00:06:29,940 --> 00:06:32,920
You're still going to create a hashtable the same way.

104
00:06:32,920 --> 00:06:36,100
The only difference is we're going to stick a little tag in

105
00:06:36,100 --> 00:06:39,390
front of it called ordered and that tells PowerShell treat

106
00:06:39,390 --> 00:06:43,840
this as a special type of hashtable, an ordered hashtable.

107
00:06:43,840 --> 00:06:47,240
So now if I look at k,

108
00:06:47,240 --> 00:06:52,380
the keys are listed in the same order that I entered them or

109
00:06:52,380 --> 00:06:55,290
created them in the hashtable so I've got first,

110
00:06:55,290 --> 00:06:56,540
second, and third.

111
00:06:56,540 --> 00:07:01,240
Technically, this is a different type of object than the hashtable.

112
00:07:01,240 --> 00:07:06,030
Now you can see this is a ordered dictionary object which is different

113
00:07:06,030 --> 00:07:09,600
technically from a .NET perspective than a hashtable.

114
00:07:09,600 --> 00:07:12,050
However, we still use them the same way.

115
00:07:12,050 --> 00:07:16,320
You can still reference items by their property names,

116
00:07:16,320 --> 00:07:19,740
and if you want, you can change the value.

117
00:07:19,740 --> 00:07:21,750
So nothing really changes in that regard.

118
00:07:21,750 --> 00:07:27,590
The only thing that changes is that the order of the key names is different

119
00:07:27,590 --> 00:07:32,140
if you use ordered versus just a traditional hashtable.

120
00:07:32,140 --> 00:07:39,640
Of course, you should read the help topic about hashtables to learn even more.

121
00:07:39,640 --> 00:07:43,940
Next let me show you some practical ways to use hashtables.

122
00:07:43,940 --> 00:07:47,640
The first step is splatting, and this is how splatting works.

123
00:07:47,640 --> 00:07:47,990
First,

124
00:07:47,990 --> 00:07:51,450
let me show you a command like Get‑Service that you would

125
00:07:51,450 --> 00:07:53,570
normally run and this is using parameters,

126
00:07:53,570 --> 00:07:56,140
‑name and ‑exclude.

127
00:07:56,140 --> 00:07:59,600
So I'm going to get all the services that start with the letter B,

128
00:07:59,600 --> 00:08:02,140
but exclude the bits service.

129
00:08:02,140 --> 00:08:07,760
Now what I'm going to do is define a hashtable and the keys of the hashtable,

130
00:08:07,760 --> 00:08:13,670
Name and Exclude, are going to match the name of the parameters for Get‑Service,

131
00:08:13,670 --> 00:08:16,930
Name and Exclude, and the values, as you can see,

132
00:08:16,930 --> 00:08:19,240
are also identical.

133
00:08:19,240 --> 00:08:22,370
And this splatting technique works whether the parameter is

134
00:08:22,370 --> 00:08:26,560
positional as name is or if it is required for you to type the

135
00:08:26,560 --> 00:08:28,690
name if you want to use it like exclude.

136
00:08:28,690 --> 00:08:32,400
So $p is a hashtable to name value pairs.

137
00:08:32,400 --> 00:08:35,280
The name matches the parameter name and the value

138
00:08:35,280 --> 00:08:37,640
matches the value that you want to give.

139
00:08:37,640 --> 00:08:43,130
So now what I can do is I can run Get‑Service and splat the hashtable.

140
00:08:43,130 --> 00:08:46,540
Now splatting, you can see I get the same result.

141
00:08:46,540 --> 00:08:48,640
The syntax is a little bit different.

142
00:08:48,640 --> 00:08:49,780
Pay close attention here.

143
00:08:49,780 --> 00:08:53,110
Notice I'm using the @ symbol and then the name of

144
00:08:53,110 --> 00:08:56,040
the variable that has my hashtable, $p.

145
00:08:56,040 --> 00:08:59,060
There is no dollar sign, it's just @p,

146
00:08:59,060 --> 00:09:04,120
and so it looks for key names that match up to your parameter names

147
00:09:04,120 --> 00:09:07,300
and then joins those things together and then everything runs the

148
00:09:07,300 --> 00:09:09,350
way that you would normally expect.

149
00:09:09,350 --> 00:09:11,490
Now a reason this is really useful,

150
00:09:11,490 --> 00:09:14,930
and this maybe something that's more important when you get to scripting,

151
00:09:14,930 --> 00:09:18,150
is you can rebuild these hashtables kind of on the fly.

152
00:09:18,150 --> 00:09:20,340
Now I'm going to do this in the console.

153
00:09:20,340 --> 00:09:24,450
Let's create another hashtable and this is a hashtable

154
00:09:24,450 --> 00:09:26,640
that I can use with Get‑CimInstance.

155
00:09:26,640 --> 00:09:28,840
Now in this particular hashtable,

156
00:09:28,840 --> 00:09:33,900
I'm using the ‑verbose parameter because that is a switch that I want to enable.

157
00:09:33,900 --> 00:09:39,640
In a hashtable, I'll use a value of $true, and there you can see the hashtable.

158
00:09:39,640 --> 00:09:43,710
So now I can run Get‑CimInstance and splat $cim.

159
00:09:43,710 --> 00:09:45,480
Notice again, @cim.

160
00:09:45,480 --> 00:09:49,620
I get the verbose output and you can see the results there.

161
00:09:49,620 --> 00:09:52,360
Now though, I can easily modify the hashtable.

162
00:09:52,360 --> 00:09:56,340
Let's say I want to get the win32 operating system.

163
00:09:56,340 --> 00:10:00,340
So if I just change the Classname parameter, everything else remains the same.

164
00:10:00,340 --> 00:10:03,600
And now I can rerun the command, again splatting,

165
00:10:03,600 --> 00:10:08,220
and I get a different result pretty much using the same hashtable.

166
00:10:08,220 --> 00:10:10,890
All I did was just change what needed to be changed.

167
00:10:10,890 --> 00:10:12,260
Again, when you get to scripting,

168
00:10:12,260 --> 00:10:17,840
I think this is a technique that you will be using often.

169
00:10:17,840 --> 00:10:23,240
If you need to though, you can also specify additional parameters.

170
00:10:23,240 --> 00:10:23,830
In this case,

171
00:10:23,830 --> 00:10:28,110
I'm combining the hashtable and then I'm also specifying

172
00:10:28,110 --> 00:10:30,100
an additional parameter ‑ComputerName.

173
00:10:30,100 --> 00:10:34,310
So now I'm acquiring a remote computer to get the operating system information.

174
00:10:34,310 --> 00:10:34,760
Again,

175
00:10:34,760 --> 00:10:44,000
this is something that you will likely use more when you get to scripting and there is a help topic that you should take a look at.

