Hi, everyone, and welcome back.
Thanks for sticking with me.
In this lesson, we are going to look at working with objects in PowerShell.
When I teach PowerShell,
I'm always faced with kind of a chicken and egg situation.
I want to teach you something but sometimes I have to use something that I
haven't taught you yet in order to teach you something,
so which do I do first?
Well, I've been talking about objects,
and we've been using them throughout this course,
but I haven't really explained them.
So in this module,
I want to spend a little more time and dive deeper into objects in PowerShell.
So what is an object?
Well,
an object from our point of view, as IT pros, is just a representation in
software of something that you want to manage or work with.
That thing could be a file, could be a service,
it could be a vegetable, it could be a mailbox,
a SharePoint site.
It doesn't matter.
It's just something that has been defined through some
software mechanism or construction that represents the
thing that you want to work with.
An object should be considered a black box,
meaning we really don't care how it's constructed.
I don't care.
I don't need to know what language it's written in or how it's written,
or who wrote it, or anything like that.
All I care about is how do I work with it?
How do I decide what objects I'm going to work with?
What are those, what do they look like?
How do I take advantage of them?
Those are the things that matter to me because in PowerShell,
everything that I work with is some type of object.
This is the biggest thing about learning PowerShell.
We move from a text‑based paradigm,
which is kind of what we had with batch files,
and to a certain degree VBScript, and lots of other languages.
There's nothing necessarily wrong with that approach,
but that approach is different than PowerShell PowerShell
is working with objects in the pipeline.
Once you get your head around that idea,
you can do a lot more with PowerShell,
and you'll wonder why you waited so long to learn it,
so let's dive into objects a bit deeper.
Objects can be described by their members.
There are three types of members we primarily deal with in PowerShell,
well, actually, only two.
There is a property.
A property is just something that describes the object,
color, size, status, working set size.
Those are all properties.
Sometimes you can change the property, and sometimes the property is read‑only.
Many of the cmdlets that you work with in PowerShell take advantage and
leverage the property names of a particular object.
Methods are things that we can do to the object or have the object do.
We can stop a process, we can start a service, we can create a file.
Those are all methods.
Now,
we don't have to be .NET developers to use PowerShell because there are
cmdlets that often take advantage and use these methods for us.
The other member type that you'll see is an event.
Windows is an event‑driven operating system.
Things are closing, and opening, and starting, and exiting.
When an event happens, it is set to fire,
and then Windows is designed to react to those
different events when you click a button,
for example.
Although when it comes to working with objects,
we really won't be working with events.
Now, as I said, everything in PowerShell is an object.
For the most part, you know some of these object types already.
These are just a few of the ones that you'll probably
reference all the time or use all the time.
Things like strings, which is just text,
Int32, there's also Int64, which are different size numbers.
You don't have to be a dominant developer.
You'll actually learn a little .NET or a little C#
the more you work with PowerShell.
There's a DateTime object, Boolean values,
which are true/false, or even something like XML.
PowerShell is savvy enough to know, if you type something of pipeline,
it will know what that is.
Or as I'll show you, you can tell PowerShell,
hey, make this a DateTime object or make this a number.
In fact, we have a couple operators that I refer to as the type operators,
‑Is dash and ‑As.
‑Is is a testing parameter, say, hey,
is this thing this particular type or take this thing
and treat it as that particular type.
Now, sometimes,
the As operator doesn't always work because I can't necessarily
coerce a DateTime to become an XML document,
but I could take a string and say, hey,
take this string and treat it as a DateTime object.
I'll get to that, and you'll see that in the demonstration.
Now, throughout this course,
I've been working with objects and referencing an object notation,
so let me go into this a little bit further here.
We can reference objects and the members of those objects via a dotted notation.
So if I create a variable, $s, make it a string,
foo, I can reference properties of that object using this dotted notation.
So I can say, hey, I know that the string has a length of property,
and I can reference that by taking my object,
which is held by $s.
and then the name of the member, in this case,
the length property, and PowerShell will then display the value of that property.
So it's really quite simple and easy to do.
If you pipe $s to Get‑Member,
Get‑Member will display all of the information it knows about that object,
so you can discover properties, and methods, and events.
One thing to keep in mind when you work with PowerShell, PowerShell does a lot
to abstract the sausage making and everything that goes on behind the hood, and
there are a number of things that PowerShell adds to say the native .NET
Framework to make it easier for IT pros to work with PowerShell. Microsoft did
not want you to be a .NET developer.
A lot of the initial push for PowerShell were for IT pros to say, hey, here's a
tool to help you put out that fire and to get your job done.
The cmdlets are a good start, and they work with objects,
but don't assume that everything that you see when you run a command
like Get‑Service is all there is about that object.
Get‑Member will show you everything that you need to know,
and once you discover those things, then you can use that information to do even more.