1
00:00:00,940 --> 00:00:03,020
Hi, everyone, and welcome back.

2
00:00:03,020 --> 00:00:04,350
Thanks for sticking with me.

3
00:00:04,350 --> 00:00:09,580
In this lesson, we are going to look at working with objects in PowerShell.

4
00:00:09,580 --> 00:00:10,910
When I teach PowerShell,

5
00:00:10,910 --> 00:00:14,140
I'm always faced with kind of a chicken and egg situation.

6
00:00:14,140 --> 00:00:17,600
I want to teach you something but sometimes I have to use something that I

7
00:00:17,600 --> 00:00:19,960
haven't taught you yet in order to teach you something,

8
00:00:19,960 --> 00:00:21,700
so which do I do first?

9
00:00:21,700 --> 00:00:23,250
Well, I've been talking about objects,

10
00:00:23,250 --> 00:00:26,340
and we've been using them throughout this course,

11
00:00:26,340 --> 00:00:27,610
but I haven't really explained them.

12
00:00:27,610 --> 00:00:29,050
So in this module,

13
00:00:29,050 --> 00:00:34,230
I want to spend a little more time and dive deeper into objects in PowerShell.

14
00:00:34,230 --> 00:00:35,830
So what is an object?

15
00:00:35,830 --> 00:00:36,050
Well,

16
00:00:36,050 --> 00:00:40,920
an object from our point of view, as IT pros, is just a representation in

17
00:00:40,920 --> 00:00:45,440
software of something that you want to manage or work with.

18
00:00:45,440 --> 00:00:47,690
That thing could be a file, could be a service,

19
00:00:47,690 --> 00:00:49,800
it could be a vegetable, it could be a mailbox,

20
00:00:49,800 --> 00:00:50,730
a SharePoint site.

21
00:00:50,730 --> 00:00:52,140
It doesn't matter.

22
00:00:52,140 --> 00:00:55,110
It's just something that has been defined through some

23
00:00:55,110 --> 00:00:59,130
software mechanism or construction that represents the

24
00:00:59,130 --> 00:01:01,640
thing that you want to work with.

25
00:01:01,640 --> 00:01:04,550
An object should be considered a black box,

26
00:01:04,550 --> 00:01:07,130
meaning we really don't care how it's constructed.

27
00:01:07,130 --> 00:01:08,130
I don't care.

28
00:01:08,130 --> 00:01:11,460
I don't need to know what language it's written in or how it's written,

29
00:01:11,460 --> 00:01:13,240
or who wrote it, or anything like that.

30
00:01:13,240 --> 00:01:16,240
All I care about is how do I work with it?

31
00:01:16,240 --> 00:01:19,940
How do I decide what objects I'm going to work with?

32
00:01:19,940 --> 00:01:21,520
What are those, what do they look like?

33
00:01:21,520 --> 00:01:23,160
How do I take advantage of them?

34
00:01:23,160 --> 00:01:26,610
Those are the things that matter to me because in PowerShell,

35
00:01:26,610 --> 00:01:30,140
everything that I work with is some type of object.

36
00:01:30,140 --> 00:01:32,810
This is the biggest thing about learning PowerShell.

37
00:01:32,810 --> 00:01:35,040
We move from a text‑based paradigm,

38
00:01:35,040 --> 00:01:36,890
which is kind of what we had with batch files,

39
00:01:36,890 --> 00:01:40,160
and to a certain degree VBScript, and lots of other languages.

40
00:01:40,160 --> 00:01:42,850
There's nothing necessarily wrong with that approach,

41
00:01:42,850 --> 00:01:47,590
but that approach is different than PowerShell PowerShell

42
00:01:47,590 --> 00:01:49,920
is working with objects in the pipeline.

43
00:01:49,920 --> 00:01:52,120
Once you get your head around that idea,

44
00:01:52,120 --> 00:01:54,280
you can do a lot more with PowerShell,

45
00:01:54,280 --> 00:01:56,860
and you'll wonder why you waited so long to learn it,

46
00:01:56,860 --> 00:02:01,640
so let's dive into objects a bit deeper.

47
00:02:01,640 --> 00:02:04,700
Objects can be described by their members.

48
00:02:04,700 --> 00:02:08,570
There are three types of members we primarily deal with in PowerShell,

49
00:02:08,570 --> 00:02:10,420
well, actually, only two.

50
00:02:10,420 --> 00:02:11,920
There is a property.

51
00:02:11,920 --> 00:02:16,260
A property is just something that describes the object,

52
00:02:16,260 --> 00:02:19,240
color, size, status, working set size.

53
00:02:19,240 --> 00:02:21,360
Those are all properties.

54
00:02:21,360 --> 00:02:25,800
Sometimes you can change the property, and sometimes the property is read‑only.

55
00:02:25,800 --> 00:02:29,710
Many of the cmdlets that you work with in PowerShell take advantage and

56
00:02:29,710 --> 00:02:33,100
leverage the property names of a particular object.

57
00:02:33,100 --> 00:02:38,480
Methods are things that we can do to the object or have the object do.

58
00:02:38,480 --> 00:02:43,740
We can stop a process, we can start a service, we can create a file.

59
00:02:43,740 --> 00:02:45,370
Those are all methods.

60
00:02:45,370 --> 00:02:45,740
Now,

61
00:02:45,740 --> 00:02:49,700
we don't have to be .NET developers to use PowerShell because there are

62
00:02:49,700 --> 00:02:55,240
cmdlets that often take advantage and use these methods for us.

63
00:02:55,240 --> 00:02:58,740
The other member type that you'll see is an event.

64
00:02:58,740 --> 00:03:01,740
Windows is an event‑driven operating system.

65
00:03:01,740 --> 00:03:05,040
Things are closing, and opening, and starting, and exiting.

66
00:03:05,040 --> 00:03:07,990
When an event happens, it is set to fire,

67
00:03:07,990 --> 00:03:10,640
and then Windows is designed to react to those

68
00:03:10,640 --> 00:03:12,710
different events when you click a button,

69
00:03:12,710 --> 00:03:13,460
for example.

70
00:03:13,460 --> 00:03:15,490
Although when it comes to working with objects,

71
00:03:15,490 --> 00:03:17,820
we really won't be working with events.

72
00:03:17,820 --> 00:03:21,440
Now, as I said, everything in PowerShell is an object.

73
00:03:21,440 --> 00:03:24,140
For the most part, you know some of these object types already.

74
00:03:24,140 --> 00:03:26,620
These are just a few of the ones that you'll probably

75
00:03:26,620 --> 00:03:28,570
reference all the time or use all the time.

76
00:03:28,570 --> 00:03:30,890
Things like strings, which is just text,

77
00:03:30,890 --> 00:03:35,400
Int32, there's also Int64, which are different size numbers.

78
00:03:35,400 --> 00:03:38,240
You don't have to be a dominant developer.

79
00:03:38,240 --> 00:03:41,790
You'll actually learn a little .NET or a little C#

80
00:03:41,790 --> 00:03:43,590
the more you work with PowerShell.

81
00:03:43,590 --> 00:03:46,520
There's a DateTime object, Boolean values,

82
00:03:46,520 --> 00:03:49,740
which are true/false, or even something like XML.

83
00:03:49,740 --> 00:03:54,050
PowerShell is savvy enough to know, if you type something of pipeline,

84
00:03:54,050 --> 00:03:55,390
it will know what that is.

85
00:03:55,390 --> 00:03:58,100
Or as I'll show you, you can tell PowerShell,

86
00:03:58,100 --> 00:04:02,740
hey, make this a DateTime object or make this a number.

87
00:04:02,740 --> 00:04:07,440
In fact, we have a couple operators that I refer to as the type operators,

88
00:04:07,440 --> 00:04:09,060
‑Is dash and ‑As.

89
00:04:09,060 --> 00:04:12,670
‑Is is a testing parameter, say, hey,

90
00:04:12,670 --> 00:04:18,020
is this thing this particular type or take this thing

91
00:04:18,020 --> 00:04:20,940
and treat it as that particular type.

92
00:04:20,940 --> 00:04:21,580
Now, sometimes,

93
00:04:21,580 --> 00:04:25,750
the As operator doesn't always work because I can't necessarily

94
00:04:25,750 --> 00:04:28,740
coerce a DateTime to become an XML document,

95
00:04:28,740 --> 00:04:30,620
but I could take a string and say, hey,

96
00:04:30,620 --> 00:04:34,440
take this string and treat it as a DateTime object.

97
00:04:34,440 --> 00:04:37,440
I'll get to that, and you'll see that in the demonstration.

98
00:04:37,440 --> 00:04:38,560
Now, throughout this course,

99
00:04:38,560 --> 00:04:41,950
I've been working with objects and referencing an object notation,

100
00:04:41,950 --> 00:04:44,860
so let me go into this a little bit further here.

101
00:04:44,860 --> 00:04:51,740
We can reference objects and the members of those objects via a dotted notation.

102
00:04:51,740 --> 00:04:55,450
So if I create a variable, $s, make it a string,

103
00:04:55,450 --> 00:05:03,040
foo, I can reference properties of that object using this dotted notation.

104
00:05:03,040 --> 00:05:07,340
So I can say, hey, I know that the string has a length of property,

105
00:05:07,340 --> 00:05:10,290
and I can reference that by taking my object,

106
00:05:10,290 --> 00:05:12,220
which is held by $s.

107
00:05:12,220 --> 00:05:15,720
and then the name of the member, in this case,

108
00:05:15,720 --> 00:05:21,800
the length property, and PowerShell will then display the value of that property.

109
00:05:21,800 --> 00:05:25,240
So it's really quite simple and easy to do.

110
00:05:25,240 --> 00:05:27,820
If you pipe $s to Get‑Member,

111
00:05:27,820 --> 00:05:31,870
Get‑Member will display all of the information it knows about that object,

112
00:05:31,870 --> 00:05:35,640
so you can discover properties, and methods, and events.

113
00:05:35,640 --> 00:05:39,140
One thing to keep in mind when you work with PowerShell, PowerShell does a lot

114
00:05:39,140 --> 00:05:43,550
to abstract the sausage making and everything that goes on behind the hood, and

115
00:05:43,550 --> 00:05:47,730
there are a number of things that PowerShell adds to say the native .NET

116
00:05:47,730 --> 00:05:52,440
Framework to make it easier for IT pros to work with PowerShell. Microsoft did

117
00:05:52,440 --> 00:05:54,940
not want you to be a .NET developer.

118
00:05:54,940 --> 00:05:59,360
A lot of the initial push for PowerShell were for IT pros to say, hey, here's a

119
00:05:59,360 --> 00:06:03,040
tool to help you put out that fire and to get your job done.

120
00:06:03,040 --> 00:06:06,580
The cmdlets are a good start, and they work with objects,

121
00:06:06,580 --> 00:06:10,130
but don't assume that everything that you see when you run a command

122
00:06:10,130 --> 00:06:13,040
like Get‑Service is all there is about that object.

123
00:06:13,040 --> 00:06:16,620
Get‑Member will show you everything that you need to know,

124
00:06:16,620 --> 00:06:22,000
and once you discover those things, then you can use that information to do even more.

