1
00:00:01,040 --> 00:00:03,930
One of the great things I love about PowerShell is,

2
00:00:03,930 --> 00:00:07,290
in fact, that I work with objects in the pipeline,

3
00:00:07,290 --> 00:00:09,550
and this is something that you want to keep in the back

4
00:00:09,550 --> 00:00:11,510
of your head, always be thinking,

5
00:00:11,510 --> 00:00:13,550
how am I going to take advantage of this object?

6
00:00:13,550 --> 00:00:14,540
What can I do with it?

7
00:00:14,540 --> 00:00:16,100
How can I do more with it?

8
00:00:16,100 --> 00:00:19,440
And one of those things that I think you'll use often is you

9
00:00:19,440 --> 00:00:21,770
can create custom properties that you need.

10
00:00:21,770 --> 00:00:26,840
You don't have to rely on the default output that a command gives you.

11
00:00:26,840 --> 00:00:29,820
If you need something else, you can probably create it,

12
00:00:29,820 --> 00:00:31,310
and it's really not that difficult.

13
00:00:31,310 --> 00:00:32,980
You can do it all from a prompt.

14
00:00:32,980 --> 00:00:37,140
You don't have to be a developer in order to extend PowerShell.

15
00:00:37,140 --> 00:00:37,560
In fact,

16
00:00:37,560 --> 00:00:40,270
you can create custom objects right from the PowerShell

17
00:00:40,270 --> 00:00:43,900
prompt that you can then use in the PowerShell pipeline to

18
00:00:43,900 --> 00:00:45,570
do other things that you want.

19
00:00:45,570 --> 00:00:49,530
I love this idea about PowerShell being a tool that I can make

20
00:00:49,530 --> 00:00:52,620
work the way that I need it to get my job done.

21
00:00:52,620 --> 00:00:57,340
I'm not relying necessarily on someone else to do that.

22
00:00:57,340 --> 00:01:00,660
For example, if I want to create a custom property,

23
00:01:00,660 --> 00:01:02,720
and I'm going to have some demonstrations here in a moment,

24
00:01:02,720 --> 00:01:06,740
we'll go into this in much more detail, but let me give you an example here.

25
00:01:06,740 --> 00:01:10,740
One way to create custom properties is with Select‑Object.

26
00:01:10,740 --> 00:01:15,140
In my example here, I'm giving all of the files under the data directory,

27
00:01:15,140 --> 00:01:17,320
and then I'm selecting some key properties,

28
00:01:17,320 --> 00:01:19,710
so we've already seen Select‑Object, so I'm saying,

29
00:01:19,710 --> 00:01:22,640
hey, give me the Fullname, the Name, and the LastWriteTime.

30
00:01:22,640 --> 00:01:25,240
After that part, some custom properties,

31
00:01:25,240 --> 00:01:28,220
and the custom properties are defined with a hash table,

32
00:01:28,220 --> 00:01:31,040
and we've talked about hash tables earlier in the course.

33
00:01:31,040 --> 00:01:34,080
So the hash table that you use to create a custom

34
00:01:34,080 --> 00:01:40,240
property has two predefined keys, Name and Expression.

35
00:01:40,240 --> 00:01:42,660
The Name will be the Name of the custom property,

36
00:01:42,660 --> 00:01:46,140
so I'm creating a custom property called Size,

37
00:01:46,140 --> 00:01:48,970
and the Expression value will be a script block,

38
00:01:48,970 --> 00:01:51,470
and we looked at script blocks also earlier in the course.

39
00:01:51,470 --> 00:01:55,390
The script block will execute once, in this case,

40
00:01:55,390 --> 00:02:00,040
for every object in the pipeline, and that's what $_ is showing.

41
00:02:00,040 --> 00:02:05,240
So the Size property will have a value of the Length

42
00:02:05,240 --> 00:02:09,440
property of every file object in the pipeline.

43
00:02:09,440 --> 00:02:14,950
The Computername property will have a value of the environmental variable,

44
00:02:14,950 --> 00:02:17,140
computername.

45
00:02:17,140 --> 00:02:19,440
Now, here's really the fun part.

46
00:02:19,440 --> 00:02:23,340
I can create something out of thin air, so I'm going to create,

47
00:02:23,340 --> 00:02:25,610
because the purpose of my little command here is I want to

48
00:02:25,610 --> 00:02:27,240
basically create a little audit trail and say,

49
00:02:27,240 --> 00:02:30,140
what's the status of the files on a particular date,

50
00:02:30,140 --> 00:02:32,480
so I'm going to create a new property called Audit,

51
00:02:32,480 --> 00:02:37,460
and the value of that property will be Get‑Date,

52
00:02:37,460 --> 00:02:39,750
and I'm formatting it as a string because this is all

53
00:02:39,750 --> 00:02:42,610
going to be exported to a CSV file anyway.

54
00:02:42,610 --> 00:02:47,140
PowerShell and the ability to create a custom property allows me to get

55
00:02:47,140 --> 00:02:50,910
more information that's relevant to what I want to do.

56
00:02:50,910 --> 00:02:54,850
And by the way, when I use these things with Select‑Object,

57
00:02:54,850 --> 00:02:58,140
this is also going to create a custom object,

58
00:02:58,140 --> 00:03:01,740
although here's another way that you can create a custom object.

59
00:03:01,740 --> 00:03:04,550
So I'm going to run Get‑Process and Get‑Service and

60
00:03:04,550 --> 00:03:06,540
save those things to our variables.

61
00:03:06,540 --> 00:03:07,790
Now, I'm creating a hash table,

62
00:03:07,790 --> 00:03:13,650
$h, and in $h, the keys and values will become an object, and the

63
00:03:13,650 --> 00:03:17,000
way that I create that object, this is one way, is using the new

64
00:03:17,000 --> 00:03:22,630
object cmdlet, use the PSObject TypeName, and then the property

65
00:03:22,630 --> 00:03:24,640
can just be that hash table.

66
00:03:24,640 --> 00:03:30,440
Any hash table I can turn into an actual PowerShell object, just like that.

67
00:03:30,440 --> 00:03:36,000
When I get to the demonstration, I'll also show you how to use the pscustomobject shortcut.

