1
00:00:02,040 --> 00:00:05,870
I suppose this is as good a time as any to go back to PowerShell and let

2
00:00:05,870 --> 00:00:08,580
me demonstrate how you can create custom properties,

3
00:00:08,580 --> 00:00:09,500
custom objects,

4
00:00:09,500 --> 00:00:14,840
and why you're going to find this of value in your PowerShell work.

5
00:00:14,840 --> 00:00:18,060
Working with custom objects and properties is really probably

6
00:00:18,060 --> 00:00:21,060
my favorite part about being in PowerShell.

7
00:00:21,060 --> 00:00:23,630
It almost seems like magic to me.

8
00:00:23,630 --> 00:00:27,640
It may take a bit to get your head around some of these concepts,

9
00:00:27,640 --> 00:00:29,290
but once you understand how they work,

10
00:00:29,290 --> 00:00:32,940
you will find yourself using them all of the time.

11
00:00:32,940 --> 00:00:36,990
Let me give you an example of some ways you can create custom properties

12
00:00:36,990 --> 00:00:40,640
and custom objects and why you might want to do that.

13
00:00:40,640 --> 00:00:43,890
So let's say that I want to use the Get‑TimeZone command,

14
00:00:43,890 --> 00:00:46,060
so here's what it looks like by default,

15
00:00:46,060 --> 00:00:49,140
but, you know, I want a little more than that,

16
00:00:49,140 --> 00:00:50,620
so I'm going to rerun the command,

17
00:00:50,620 --> 00:00:54,840
and I'm going to select all of the properties because I'd like that output,

18
00:00:54,840 --> 00:00:59,040
but I now want to create an additional custom property.

19
00:00:59,040 --> 00:01:02,040
The property name is defined with my custom hash table,

20
00:01:02,040 --> 00:01:05,510
which has two predefined keys, Name and Expression.

21
00:01:05,510 --> 00:01:09,740
So the Name of my property is going to be Computername,

22
00:01:09,740 --> 00:01:13,320
and the Expression will be the code inside the script block,

23
00:01:13,320 --> 00:01:18,040
and in this case, I'm just returning the computername environmental variable.

24
00:01:18,040 --> 00:01:18,800
When I run this,

25
00:01:18,800 --> 00:01:21,790
I now get a custom object really that has all of

26
00:01:21,790 --> 00:01:24,860
the properties from Get‑TimeZone, plus the custom property,

27
00:01:24,860 --> 00:01:29,040
Computername, that I created.

28
00:01:29,040 --> 00:01:33,340
Here's an example that does a little bit more than that.

29
00:01:33,340 --> 00:01:34,890
So let's do Get‑Process,

30
00:01:34,890 --> 00:01:39,280
and I'm going to filter where all the processes have a

31
00:01:39,280 --> 00:01:46,140
working set size greater or equal to 50 MB,

32
00:01:46,140 --> 00:01:49,610
and now I'll start selecting the object properties that I want to keep,

33
00:01:49,610 --> 00:01:56,340
such as Id and Name, and now I'm going to create a custom property.

34
00:01:56,340 --> 00:01:58,150
So I'm going to create a custom property,

35
00:01:58,150 --> 00:02:01,110
and you should start working at trying to decipher

36
00:02:01,110 --> 00:02:03,540
the code that's on the screen here.

37
00:02:03,540 --> 00:02:06,580
So the property name will be called MemoryMB,

38
00:02:06,580 --> 00:02:10,110
the value is the code inside the script blocks,

39
00:02:10,110 --> 00:02:13,670
and I'm taking the current object in the pipeline,

40
00:02:13,670 --> 00:02:19,560
that's a $_, that object's working set property using the alias property,

41
00:02:19,560 --> 00:02:28,760
ws, so I've got $_.ws / 1 MB, that's in parentheses,

42
00:02:28,760 --> 00:02:32,170
so PowerShell will treat that as 1 unit,

43
00:02:32,170 --> 00:02:36,240
and then I'm going to treat that as an int32.

44
00:02:36,240 --> 00:02:38,640
This will have the effect of rounding it to an integer

45
00:02:38,640 --> 00:02:42,540
and eliminating any decimal points.

46
00:02:42,540 --> 00:02:47,450
I'm now going to create another property called Threads that will give me the

47
00:02:47,450 --> 00:02:52,340
count of the number of threads associated with each process,

48
00:02:52,340 --> 00:02:57,340
and now I'm going to create a property called Runtime,

49
00:02:57,340 --> 00:03:00,810
and the value of Runtime will be the current datetime

50
00:03:00,810 --> 00:03:05,640
minus when the process started.

51
00:03:05,640 --> 00:03:09,660
So again, the Starttime is a property of a Process object,

52
00:03:09,660 --> 00:03:12,400
$_ means the current object in the pipeline,

53
00:03:12,400 --> 00:03:16,500
and I'm just referencing the property with that dotted notation.

54
00:03:16,500 --> 00:03:20,140
And then just to make it nice and easy to read,

55
00:03:20,140 --> 00:03:23,240
I'll pipe the whole results to Format‑Table,

56
00:03:23,240 --> 00:03:23,820
and there we go.

57
00:03:23,820 --> 00:03:29,450
So there are all the processes with a working set size greater than 50 MB,

58
00:03:29,450 --> 00:03:33,860
I see my ID, Name, and the custom properties that I created,

59
00:03:33,860 --> 00:03:35,040
MemoryMB.

60
00:03:35,040 --> 00:03:40,410
Because I treated it as an int32, it rounded everything to the nearest integer,

61
00:03:40,410 --> 00:03:44,240
so I get a nice, clean result.

62
00:03:44,240 --> 00:03:47,180
Here's another example about why you need to read the help,

63
00:03:47,180 --> 00:03:51,840
and we're going to do even more with creating things on the fly.

64
00:03:51,840 --> 00:03:54,840
So I'm going to take the numbers 1 through 10 and sort them,

65
00:03:54,840 --> 00:04:00,150
but I'm going to sort them on a custom property that doesn't even exist.

66
00:04:00,150 --> 00:04:02,060
And, in fact, I don't even really have a name for it.

67
00:04:02,060 --> 00:04:05,040
It's just basically going to take each number,

68
00:04:05,040 --> 00:04:07,200
and I'm using the modulo operator.

69
00:04:07,200 --> 00:04:08,910
That's the percent symbol.

70
00:04:08,910 --> 00:04:10,220
This is a math operation,

71
00:04:10,220 --> 00:04:12,960
which will tell you whether there are remaining numbers.

72
00:04:12,960 --> 00:04:16,750
So dividing a number by 2 will give me even numbers first

73
00:04:16,750 --> 00:04:19,580
and then odd numbers because there's no remainder when I

74
00:04:19,580 --> 00:04:22,540
divide by 2 on an even number.

75
00:04:22,540 --> 00:04:24,680
So now I have the numbers 1 through 10 basically

76
00:04:24,680 --> 00:04:29,270
sorted whether they're even or odd, kind of an artificial example,

77
00:04:29,270 --> 00:04:32,740
other than the fact I wanted to show you that there's really no limit

78
00:04:32,740 --> 00:04:35,470
to what you can do with these custom properties.

79
00:04:35,470 --> 00:04:40,940
And again, that $_ modulo 2 is in a script block,

80
00:04:40,940 --> 00:04:46,140
so you can use these script blocks to kind of define things on the fly.

81
00:04:46,140 --> 00:04:48,440
Let me give you another example.

82
00:04:48,440 --> 00:04:51,950
Let's run Get‑ChildItem on my scripts folder and get

83
00:04:51,950 --> 00:04:57,040
just the top‑level directories.

84
00:04:57,040 --> 00:05:03,640
I'm going to sort those top‑level directories this way.

85
00:05:03,640 --> 00:05:08,040
I'm creating a custom property, and in this custom property,

86
00:05:08,040 --> 00:05:14,590
I'm going to get all of the files in each top‑level folder recursively

87
00:05:14,590 --> 00:05:19,940
and then get the count property of all of those files,

88
00:05:19,940 --> 00:05:26,140
and I'm going to then do this sort in descending order.

89
00:05:26,140 --> 00:05:31,940
Then I will select the first 5 objects that come through the pipeline.

90
00:05:31,940 --> 00:05:37,140
So these are the top five directories that have the most files in them.

91
00:05:37,140 --> 00:05:40,570
Again, maybe not something that you need on a production level,

92
00:05:40,570 --> 00:05:46,250
but I want you to understand the process that I used so that

93
00:05:46,250 --> 00:05:48,660
when you need to do something similar, you go,

94
00:05:48,660 --> 00:05:53,340
oh, yeah, Jeff showed me how to create a custom property to sort on,

95
00:05:53,340 --> 00:05:57,640
and you understand how it works and why you might want to use it.

96
00:05:57,640 --> 00:06:01,740
We can do a similar thing with grouping.

97
00:06:01,740 --> 00:06:05,040
So I'm going to get all of my ps1 files,

98
00:06:05,040 --> 00:06:08,430
and I'm going to group the results based on the year of the

99
00:06:08,430 --> 00:06:12,440
LastWriteTime property of the current object in the pipeline.

100
00:06:12,440 --> 00:06:18,730
So every file that comes through, I'm going to group on this custom property,

101
00:06:18,730 --> 00:06:26,540
so now I can see how many items I have and the corresponding year.

102
00:06:26,540 --> 00:06:29,080
So that's creating custom properties.

103
00:06:29,080 --> 00:06:33,540
Let's look now at custom objects.

104
00:06:33,540 --> 00:06:37,380
So I'm going to create a custom object with some new properties.

105
00:06:37,380 --> 00:06:40,270
One of them will be all of the processes.

106
00:06:40,270 --> 00:06:44,020
I'm going to use Get‑CimInstance, I'll do a similar thing for services,

107
00:06:44,020 --> 00:06:50,340
and I just want to get the ones that are running.

108
00:06:50,340 --> 00:06:53,440
That'll take a moment to run.

109
00:06:53,440 --> 00:06:56,650
Now I'm going to create a hash table, so I'm creating a variable,

110
00:06:56,650 --> 00:07:01,740
$hash, to hold the results.

111
00:07:01,740 --> 00:07:04,050
So my first key is going to call the Computername.

112
00:07:04,050 --> 00:07:07,740
I'll use the environmental variable for computername,

113
00:07:07,740 --> 00:07:11,800
I will grab the Version number from the PSVersion table,

114
00:07:11,800 --> 00:07:15,740
here's another example of the dotted notation,

115
00:07:15,740 --> 00:07:20,940
my ProcessCount, I'll do a similar thing with the Services,

116
00:07:20,940 --> 00:07:23,680
and now I'm going to run the Get‑Uptime command,

117
00:07:23,680 --> 00:07:28,740
which is in PowerShell 7, and close the hash table.

118
00:07:28,740 --> 00:07:34,340
So the hash table looks like the hash table that we have seen before.

119
00:07:34,340 --> 00:07:35,430
Now the fun part.

120
00:07:35,430 --> 00:07:38,640
I want to turn this into an actual object.

121
00:07:38,640 --> 00:07:42,860
I can use New‑Object and use the PSObject as the type name,

122
00:07:42,860 --> 00:07:47,540
which is kind of the base, if you will, and then say use the hash table,

123
00:07:47,540 --> 00:07:51,120
$hash, for all of the properties,

124
00:07:51,120 --> 00:07:55,040
and I'm just going to tee this to a variable $new so I can

125
00:07:55,040 --> 00:07:58,140
do something else with it in a moment.

126
00:07:58,140 --> 00:07:58,820
There you go.

127
00:07:58,820 --> 00:08:03,010
So you can see the properties and the names are the same as in the hash table,

128
00:08:03,010 --> 00:08:05,740
but this is structured as an object.

129
00:08:05,740 --> 00:08:07,890
In fact, my pipe $new,

130
00:08:07,890 --> 00:08:13,410
which I saved because I tee‑ed it to Get‑Member, you can see that this

131
00:08:13,410 --> 00:08:17,940
is a System.Management.Automation.PSCustomObject.

132
00:08:17,940 --> 00:08:21,070
There are some advanced ways that you can insert type names,

133
00:08:21,070 --> 00:08:25,740
but for now, we're going to leave it as a generic custom object.

134
00:08:25,740 --> 00:08:28,130
Because it's a custom object, I can write to the pipeline.

135
00:08:28,130 --> 00:08:30,660
So if I wanted to, I could send this to Out‑File,

136
00:08:30,660 --> 00:08:37,740
or Out‑Printer, or Export‑CSV just about anything that I would want to do.

137
00:08:37,740 --> 00:08:39,940
The other way that you could do this,

138
00:08:39,940 --> 00:08:42,740
and the way that I tend to do it when I get into scripting,

139
00:08:42,740 --> 00:08:45,840
would be an approach like this.

140
00:08:45,840 --> 00:08:49,040
So I'm going to create a variable, $obj,

141
00:08:49,040 --> 00:08:51,030
and I'm going to still create a hash table,

142
00:08:51,030 --> 00:08:58,840
but in front of the hash table, I'm putting this pscustomobject type accelerator.

143
00:08:58,840 --> 00:09:04,790
So I'm going to put in my same properties and values that I had before,

144
00:09:04,790 --> 00:09:08,130
still creating the hash table,

145
00:09:08,130 --> 00:09:12,430
except PowerShell knows to create this as a custom object,

146
00:09:12,430 --> 00:09:15,140
and you'll see this here in just a second.

147
00:09:15,140 --> 00:09:21,940
I'm going to go ahead and add the TimeZone to this object, and we'll do Uptime.

148
00:09:21,940 --> 00:09:25,530
So now, what do I have for $obj?

149
00:09:25,530 --> 00:09:27,910
So there's my custom object.

150
00:09:27,910 --> 00:09:30,420
When I created the hash table with New‑Object,

151
00:09:30,420 --> 00:09:35,240
the property names are not in any particular order.

152
00:09:35,240 --> 00:09:38,180
When you use a custom object, as I've done here,

153
00:09:38,180 --> 00:09:41,370
the property names will be displayed in the order that

154
00:09:41,370 --> 00:09:43,970
you entered them into the hash table.

155
00:09:43,970 --> 00:09:46,910
That may make a difference to you as to what you're doing.

156
00:09:46,910 --> 00:09:52,220
There are additional scripting techniques that you can use to format objects.

157
00:09:52,220 --> 00:09:54,870
That's beyond the scope of what we can do here.

158
00:09:54,870 --> 00:09:59,750
But if you need to have some control over the order of the property names,

159
00:09:59,750 --> 00:10:01,960
create a custom object.

160
00:10:01,960 --> 00:10:06,760
And this also is much nicer because I don't have to create the hash table and

161
00:10:06,760 --> 00:10:11,540
then take the second step of creating the custom object.

162
00:10:11,540 --> 00:10:16,040
Let me wrap up by revisiting an example that we just did a moment ago.

163
00:10:16,040 --> 00:10:19,240
I'm going to create a custom object, as it did earlier with Select object,

164
00:10:19,240 --> 00:10:19,920
but this time,

165
00:10:19,920 --> 00:10:23,310
I'm going to create a PSCustomObject using the type

166
00:10:23,310 --> 00:10:25,440
accelerator I just showed you.

167
00:10:25,440 --> 00:10:29,940
So let me get all the processes that are greater than

168
00:10:29,940 --> 00:10:36,540
50 MB for the working set value, use the foreach keyword,

169
00:10:36,540 --> 00:10:40,340
which is another way of looping through a collection of objects.

170
00:10:40,340 --> 00:10:45,850
So for each $p, which would be the current object in the array of $procs,

171
00:10:45,850 --> 00:10:50,540
I am going to create a custom object using the hash table and the

172
00:10:50,540 --> 00:10:55,040
pscustomobject type accelerator we just looked at.

173
00:10:55,040 --> 00:10:58,340
Now I can use the same Select object,

174
00:10:58,340 --> 00:11:02,050
and the hash table values that I used at the beginning of the demo,

175
00:11:02,050 --> 00:11:03,940
I can use them here.

176
00:11:03,940 --> 00:11:10,340
So I'll get the Id, I'll get the name, I'll create my MemoryMB property name,

177
00:11:10,340 --> 00:11:13,180
and I'm using for the value the same code that was in the

178
00:11:13,180 --> 00:11:17,040
Expression block in the Select object.

179
00:11:17,040 --> 00:11:22,640
We can do Threads, we'll get that thread.count again, and the Runtime.

180
00:11:22,640 --> 00:11:24,190
Now, the one difference that I have made,

181
00:11:24,190 --> 00:11:28,440
by the way, versus the previous example with Select object,

182
00:11:28,440 --> 00:11:30,700
Select object was doing this all in line,

183
00:11:30,700 --> 00:11:37,150
so it's using $_, in this case, I'm using the foreach keyword,

184
00:11:37,150 --> 00:11:40,610
so this enumerator allows me to create my own variable, and

185
00:11:40,610 --> 00:11:46,140
that's why I'm using $p, so it's not $_. It's $p.

186
00:11:46,140 --> 00:11:50,840
Now, let's continue on here and get the Computername.

187
00:11:50,840 --> 00:11:54,990
Now, since I hit Enter, PowerShell will process these things and create

188
00:11:54,990 --> 00:12:03,090
a custom object for every item that's in $procs, and there it is. This

189
00:12:03,090 --> 00:12:07,770
is a, to my way of thinking, sometimes an easier way if I'm creating a

190
00:12:07,770 --> 00:12:11,670
custom object, and this is something I use often in my scripting, this

191
00:12:11,670 --> 00:12:16,040
is a much easier way to create an object I think is easier to read than

192
00:12:16,040 --> 00:12:20,290
using Select objects. If you were doing something at the console,

193
00:12:20,290 --> 00:12:21,080
kind of a one‑off,

194
00:12:21,080 --> 00:12:25,340
I need to quickly bang this out, Select object with those hash tables

195
00:12:25,340 --> 00:12:29,380
worked perfectly fine. For more long‑term maintainability,

196
00:12:29,380 --> 00:12:31,470
especially, as I said, when you get the scripting, you

197
00:12:31,470 --> 00:12:36,140
might find this approach much easier.

198
00:12:36,140 --> 00:12:39,520
Always be thinking about objects and working with objects in the

199
00:12:39,520 --> 00:12:43,530
pipeline, Whether you're working with objects in dotted notation or

200
00:12:43,530 --> 00:12:46,010
creating custom properties or custom objects,

201
00:12:46,010 --> 00:12:47,910
this is really important stuff.

202
00:12:47,910 --> 00:12:51,140
Look for ways to take advantage of these things.

203
00:12:51,140 --> 00:12:55,380
I hope you now feel a little more comfortable about knowing what an object is,

204
00:12:55,380 --> 00:12:56,520
how to discover it.

205
00:12:56,520 --> 00:12:59,740
Get‑Member is your friend. Grab the downloads,

206
00:12:59,740 --> 00:13:02,740
give all this to try, and see for yourself.

207
00:13:02,740 --> 00:13:04,080
Thanks for watching this lesson.

208
00:13:04,080 --> 00:13:14,000
So come back for one more fun‑filled module where we learn more about taking advantage and learning about the PowerShell 7 language.

