1
00:00:00,340 --> 00:00:03,720
Okay, so now that we've worked with standard data values,

2
00:00:03,720 --> 00:00:08,340
variables, and string values, let's talk about custom object data.

3
00:00:08,340 --> 00:00:11,780
The advantage here is that we want to be able to create something

4
00:00:11,780 --> 00:00:14,750
custom that matches the type of data that we're using.

5
00:00:14,750 --> 00:00:19,240
Now, a custom data object could be an array.

6
00:00:19,240 --> 00:00:21,210
It could be a hash table.

7
00:00:21,210 --> 00:00:24,440
It could also be a custom object.

8
00:00:24,440 --> 00:00:26,160
So, what are arrays?

9
00:00:26,160 --> 00:00:29,720
Well, an array is a list or collection of values or objects,

10
00:00:29,720 --> 00:00:33,660
and we've seen some of these already where they're a comma‑separated list.

11
00:00:33,660 --> 00:00:39,880
Most important thing here, arrays only contain values, no properties.

12
00:00:39,880 --> 00:00:43,760
So there's no something on an item dot something as a property.

13
00:00:43,760 --> 00:00:45,260
That doesn't exist.

14
00:00:45,260 --> 00:00:47,410
Now to create an array,

15
00:00:47,410 --> 00:00:53,640
empty arrays are created by using the @ and then parentheses.

16
00:00:53,640 --> 00:00:56,760
You can have comma‑separated lists as arrays,

17
00:00:56,760 --> 00:01:02,580
and then, of course, the Write‑Output command can also create string arrays.

18
00:01:02,580 --> 00:01:04,870
So if you're just using string arrays of data,

19
00:01:04,870 --> 00:01:08,040
there's already a command that can help us do that.

20
00:01:08,040 --> 00:01:14,940
So to create an array, an empty array, it's going to be $variable = @().

21
00:01:14,940 --> 00:01:20,090
That will create an array, and then we can add values into that array.

22
00:01:20,090 --> 00:01:23,430
We could create an array with existing values.

23
00:01:23,430 --> 00:01:26,220
So I'm using January, February, March, April, May, June.

24
00:01:26,220 --> 00:01:29,910
They are in text values and comma‑separated,

25
00:01:29,910 --> 00:01:34,240
so that populates my variable array automatically.

26
00:01:34,240 --> 00:01:39,890
I could create an array with text values, but not use the @ and parentheses.

27
00:01:39,890 --> 00:01:43,840
That would create a standard string array.

28
00:01:43,840 --> 00:01:46,820
And then I can also use the Write‑Output to do the same thing.

29
00:01:46,820 --> 00:01:48,660
So we're not using the @(),

30
00:01:48,660 --> 00:01:52,460
we're just using Write‑Output and these values with separations,

31
00:01:52,460 --> 00:01:55,030
and that will create an array.

32
00:01:55,030 --> 00:01:57,520
Now, of course, once we have the arrays,

33
00:01:57,520 --> 00:02:01,940
then, of course, we need to look at how we retrieve array items.

34
00:02:01,940 --> 00:02:06,380
So I'm using $variable = @() and then January,

35
00:02:06,380 --> 00:02:07,520
February, March, April, May, June.

36
00:02:07,520 --> 00:02:09,720
If I wish to retrieve a value,

37
00:02:09,720 --> 00:02:13,510
the most standard and easiest way is to use what's called Offset.

38
00:02:13,510 --> 00:02:17,970
Offset will be the unique ID or placement of that value.

39
00:02:17,970 --> 00:02:23,340
So if I do $variable[0], that will get me January.

40
00:02:23,340 --> 00:02:28,050
If I do $variable[2], that will give me March.

41
00:02:28,050 --> 00:02:30,840
It always begins at 0.

42
00:02:30,840 --> 00:02:34,650
I can access an array item using multiple offsets as well,

43
00:02:34,650 --> 00:02:37,910
so I could say get me variable offset 0,

44
00:02:37,910 --> 00:02:42,660
January, 1, February, 4, April.

45
00:02:42,660 --> 00:02:45,570
That would actually get April or May.

46
00:02:45,570 --> 00:02:48,340
That would give me those three values.

47
00:02:48,340 --> 00:02:52,810
I can access the array items using a range operator as an offset.

48
00:02:52,810 --> 00:02:55,330
So just like we did previously where we populated a

49
00:02:55,330 --> 00:02:59,260
variable with 1 to 10 by using 1..10,

50
00:02:59,260 --> 00:03:02,280
I could do the same here and say go and get me the

51
00:03:02,280 --> 00:03:05,760
range operator offset of 2 to 5,

52
00:03:05,760 --> 00:03:09,510
which would obviously take me over to not January,

53
00:03:09,510 --> 00:03:13,430
not February, but start at March and go upwards.

54
00:03:13,430 --> 00:03:18,020
Now we also often want to iterate array items.

55
00:03:18,020 --> 00:03:22,340
So not only are we just taking odd things out and getting single values,

56
00:03:22,340 --> 00:03:23,870
we may want to iterate.

57
00:03:23,870 --> 00:03:25,530
So once again, we create the variable.

58
00:03:25,530 --> 00:03:29,440
And you can see we're using a ForEach‑Object.

59
00:03:29,440 --> 00:03:31,840
So this means I'm going into the variable.

60
00:03:31,840 --> 00:03:33,370
I'm using a pipe command,

61
00:03:33,370 --> 00:03:36,670
which we've talked about in the past where we're piping whatever's in the

62
00:03:36,670 --> 00:03:40,930
variable to the next piece and saying ForEach‑Object,

63
00:03:40,930 --> 00:03:43,550
and then I'm just literally writing out the value,

64
00:03:43,550 --> 00:03:46,160
so the month is whatever that would be.

65
00:03:46,160 --> 00:03:49,350
And you can see we're using something called $PSItem.

66
00:03:49,350 --> 00:03:56,250
That is a standard PowerShell property or variable that's created by

67
00:03:56,250 --> 00:04:00,840
PowerShell for the output of a ForEach‑Object loop.

68
00:04:00,840 --> 00:04:04,770
We can do this differently by using a foreach statement that we've used before,

69
00:04:04,770 --> 00:04:08,070
so we can say foreach item in that variable and then

70
00:04:08,070 --> 00:04:10,230
go ahead and write the values out.

71
00:04:10,230 --> 00:04:12,600
I could also use the ForEach method.

72
00:04:12,600 --> 00:04:16,220
So knowing that the variable contains the values,

73
00:04:16,220 --> 00:04:20,540
that allows me to say ForEach and then write those values out.

74
00:04:20,540 --> 00:04:21,320
And then, of course,

75
00:04:21,320 --> 00:04:25,780
our other option is to use a for loop to go through the values.

76
00:04:25,780 --> 00:04:30,260
Now you can see the code difference here, single lines to multiple lines.

77
00:04:30,260 --> 00:04:31,170
So, for example,

78
00:04:31,170 --> 00:04:34,660
that last one is foreach item equals 0 if the item

79
00:04:34,660 --> 00:04:36,380
is less than the variable count.

80
00:04:36,380 --> 00:04:41,280
So if there's seven things in there, it will iterate seven times and then stop.

81
00:04:41,280 --> 00:04:47,340
Now, another version of arrays, I suppose, is a hash table.

82
00:04:47,340 --> 00:04:51,040
So the hash table is a data structure, very much like an array,

83
00:04:51,040 --> 00:04:54,250
except you can store every value, an object,

84
00:04:54,250 --> 00:04:55,580
using a key.

85
00:04:55,580 --> 00:04:59,310
So hash tables only contain values, not properties.

86
00:04:59,310 --> 00:05:03,540
They are basically a key value store.

87
00:05:03,540 --> 00:05:05,470
So when we look at the hash tables,

88
00:05:05,470 --> 00:05:10,650
they're also known as a dictionary or what's called an associative array,

89
00:05:10,650 --> 00:05:15,840
the data structure that stores one or more key value pairs.

90
00:05:15,840 --> 00:05:21,240
So there will be a unique key that represents the value.

91
00:05:21,240 --> 00:05:26,940
Keys and values in hash tables are defined as .NET objects.

92
00:05:26,940 --> 00:05:33,440
And then, of course, it's an efficient way of finding and retrieving data.

93
00:05:33,440 --> 00:05:38,260
Now to create a hash table, we once again start with the @ as before.

94
00:05:38,260 --> 00:05:43,660
You wrap the hash table in braces, so you can see we've got our combination here.

95
00:05:43,660 --> 00:05:50,440
You can use an = to separate each key from its value,

96
00:05:50,440 --> 00:05:57,640
and then you use a semicolon or a line break to separate the key value pairs.

97
00:05:57,640 --> 00:06:00,240
So let's look at how we create the hash table.

98
00:06:00,240 --> 00:06:03,080
Well, first off, I'm going to create my empty hash table.

99
00:06:03,080 --> 00:06:04,550
Now don't get confused here.

100
00:06:04,550 --> 00:06:09,420
When we created the array, we used @, and then we used parentheses.

101
00:06:09,420 --> 00:06:16,040
A hash table is an @, and then what I refer to as the squirrelly brackets.

102
00:06:16,040 --> 00:06:20,600
What we can then do is create what is the key and values.

103
00:06:20,600 --> 00:06:27,840
So you can see here I've got Month = 5; Name = May; Season = Spring.

104
00:06:27,840 --> 00:06:31,750
I could go a little bit further here, and I could create an ordered hash table,

105
00:06:31,750 --> 00:06:40,530
which would order those values by the keys and values.

106
00:06:40,530 --> 00:06:42,860
Now to display values from the hash tables,

107
00:06:42,860 --> 00:06:46,400
we first store the hash table in a variable,

108
00:06:46,400 --> 00:06:51,740
so we basically say $variable name, and then press Enter.

109
00:06:51,740 --> 00:06:54,990
We can then use the dot notation to display all of the

110
00:06:54,990 --> 00:06:58,640
keys or the values inside that variable.

111
00:06:58,640 --> 00:07:03,420
So, for example, $variable.keys would return the key values,

112
00:07:03,420 --> 00:07:07,640
whereas $variable.values would return the values.

113
00:07:07,640 --> 00:07:10,430
Each key name is also a property.

114
00:07:10,430 --> 00:07:15,350
So, for example, $variable.Month, which would be one of the values,

115
00:07:15,350 --> 00:07:17,540
would return a value.

116
00:07:17,540 --> 00:07:21,040
I can also do that by using $variable and then square

117
00:07:21,040 --> 00:07:25,000
brackets and in text type in the value or the property name

118
00:07:25,000 --> 00:07:27,350
that we wish to retrieve the value for.

119
00:07:27,350 --> 00:07:29,770
So to iterate a hash table,

120
00:07:29,770 --> 00:07:33,440
let's say we create a country population hash table here,

121
00:07:33,440 --> 00:07:36,470
so I've got $variable equals a hash table,

122
00:07:36,470 --> 00:07:41,100
and in the hash table is four countries with their population.

123
00:07:41,100 --> 00:07:47,820
To iterate all the keys and values, I could use a ForEach‑Object loop.

124
00:07:47,820 --> 00:07:50,560
So I'm saying $variable.keys.

125
00:07:50,560 --> 00:07:57,130
So for each key, loop inside of it, and then the output of that will be,

126
00:07:57,130 --> 00:08:01,250
you can see I'm using a different syntax here where in the string

127
00:08:01,250 --> 00:08:04,370
values I have a 0 and a 1 in squirrelly brackets.

128
00:08:04,370 --> 00:08:08,330
That means I'm going to fill those values with something else,

129
00:08:08,330 --> 00:08:10,000
and you can see the ‑f,

130
00:08:10,000 --> 00:08:14,750
which basically says format the output of this in this way.

131
00:08:14,750 --> 00:08:19,150
So I want a value, something, which would be the name of the country,

132
00:08:19,150 --> 00:08:23,040
has a population of something, which is the value.

133
00:08:23,040 --> 00:08:25,850
So what you can see here is because we're using a ForEach‑Object

134
00:08:25,850 --> 00:08:28,830
and we've already told it to iterate the keys,

135
00:08:28,830 --> 00:08:34,420
I don't need to call the key value because in the ForEach‑Object,

136
00:08:34,420 --> 00:08:35,840
it's iterating them anyway.

137
00:08:35,840 --> 00:08:38,110
So I can do $_,

138
00:08:38,110 --> 00:08:43,210
which will represent the names of the countries or the keys from the hash table.

139
00:08:43,210 --> 00:08:45,550
And then you'll see in the variable option,

140
00:08:45,550 --> 00:08:47,510
I've told it to go ahead and get the variable,

141
00:08:47,510 --> 00:08:51,840
and then all goes together in a Write‑Output.

142
00:08:51,840 --> 00:08:55,080
Now we can iterate all the keys doing just a standard for

143
00:08:55,080 --> 00:08:57,450
loop like a standard regular foreach loop.

144
00:08:57,450 --> 00:09:01,180
So I can say foreach key in the keys, output,

145
00:09:01,180 --> 00:09:04,640
format the same thing, and then output the value.

146
00:09:04,640 --> 00:09:10,240
So iterating a hash table is very much like Iterating an array.

147
00:09:10,240 --> 00:09:14,230
Now we have another type of object here called the PSCustomObject.

148
00:09:14,230 --> 00:09:19,960
So PSCustomObjects are a really simple and easy way to create structured data.

149
00:09:19,960 --> 00:09:32,240
A PSCustomObject is comprised of both properties and values.

150
00:09:32,240 --> 00:09:36,520
So a PSCustomObject is like a hash table,

151
00:09:36,520 --> 00:09:43,740
except it's strongly typed as what's referred to as a PSCustomObject.

152
00:09:43,740 --> 00:09:47,740
The properties are ordered by default.

153
00:09:47,740 --> 00:09:48,600
And then, of course,

154
00:09:48,600 --> 00:09:52,310
the PSCustomObject contains what's referred to as a NoteProperty.

155
00:09:52,310 --> 00:09:57,510
So this is the same as common PowerShell objects that are part of

156
00:09:57,510 --> 00:10:00,340
the core PowerShell construct in the language.

157
00:10:00,340 --> 00:10:02,470
Now to create a custom PSObject,

158
00:10:02,470 --> 00:10:06,380
we're going to use syntax that's very similar to what we've chosen before.

159
00:10:06,380 --> 00:10:10,000
So we're going to say $variable =, and then we're going to strongly type,

160
00:10:10,000 --> 00:10:12,370
so we're going to say [PSCustomObject]@,

161
00:10:12,370 --> 00:10:14,710
and then the squirrelly brackets,

162
00:10:14,710 --> 00:10:18,060
which will create us effectively the hash table,

163
00:10:18,060 --> 00:10:21,240
but it's a PSCustomObject.

164
00:10:21,240 --> 00:10:24,620
We can also create an empty one in a different way by using what's

165
00:10:24,620 --> 00:10:28,100
called New‑Object with a ‑TypeName of PSObject,

166
00:10:28,100 --> 00:10:30,130
so two ways of doing this.

167
00:10:30,130 --> 00:10:33,040
That's actually quite a common way of doing it as well.

168
00:10:33,040 --> 00:10:36,420
Now if I want to create and populate the custom object

169
00:10:36,420 --> 00:10:38,540
using the countries that we used before,

170
00:10:38,540 --> 00:10:43,260
you can see that I just adjust the variable and say PSCustomObject,

171
00:10:43,260 --> 00:10:47,440
and then it's still the same kind of text values.

172
00:10:47,440 --> 00:10:50,580
Now if I want to add items to the custom object,

173
00:10:50,580 --> 00:10:55,750
now I can because I have this ability to add to the PSCustomObject.

174
00:10:55,750 --> 00:10:59,240
So you can see here, I'm saying choose the variable,

175
00:10:59,240 --> 00:11:03,500
pipe it out, and say add a member to the PSCustomObject.

176
00:11:03,500 --> 00:11:08,530
And the ‑MemberType is a NoteProperty, and then give it a name and a value.

177
00:11:08,530 --> 00:11:14,840
And so I'm now adding Russia and Norway into the list of countries.

178
00:11:14,840 --> 00:11:17,630
Now what about if we want to retrieve those values?

179
00:11:17,630 --> 00:11:21,830
Well, we have our standard PSCustomObject. And this time,

180
00:11:21,830 --> 00:11:25,530
I could return all the PSCustomObject key value pairs by

181
00:11:25,530 --> 00:11:28,640
just typing out the variable itself.

182
00:11:28,640 --> 00:11:32,590
If I wanted to get the country, I now have the ability to do so.

183
00:11:32,590 --> 00:11:34,200
I can say $variable.Spain.

184
00:11:34,200 --> 00:11:39,640
That's it, and it would return the value.

185
00:11:39,640 --> 00:11:43,780
We also have the ability to test the PSCustomObjects.

186
00:11:43,780 --> 00:11:49,720
So the type comparison, which would be kind of IS and ISNOT,

187
00:11:49,720 --> 00:11:54,250
are used to determine if an object has a specific type.

188
00:11:54,250 --> 00:11:58,540
So I could say is it this type, is it not this type?

189
00:11:58,540 --> 00:12:00,250
So what does that look like in PowerShell?

190
00:12:00,250 --> 00:12:05,040
Well, $variable1 = to number 10, so it's not a strongly‑typed variable.

191
00:12:05,040 --> 00:12:09,550
$variable2 is equal to 10, but it's a string version of that one.

192
00:12:09,550 --> 00:12:14,120
And $variable1 I'm trying to convert to an integer,

193
00:12:14,120 --> 00:12:18,320
so I'm saying $variable1 is an integer.

194
00:12:18,320 --> 00:12:23,700
I could also say $variable1 ‑is $variable2.GetType().

195
00:12:23,700 --> 00:12:27,740
So I'm trying to match it now and say are they the same?

196
00:12:27,740 --> 00:12:32,350
I've also got, well, $variable1, is it not an integer?

197
00:12:32,350 --> 00:12:34,040
So I can do that too.

198
00:12:34,040 --> 00:12:37,440
And then I can do ‑isnot $variable2.GetType.

199
00:12:37,440 --> 00:12:40,440
So that ‑is will allow me to check.

200
00:12:40,440 --> 00:12:41,580
Now, what does that look like,

201
00:12:41,580 --> 00:12:46,000
and what error messages do we get when we come back? We'll have a look at that in a moment as well.

