One of the great things I love about PowerShell is,
in fact, that I work with objects in the pipeline,
and this is something that you want to keep in the back
of your head, always be thinking,
how am I going to take advantage of this object?
What can I do with it?
How can I do more with it?
And one of those things that I think you'll use often is you
can create custom properties that you need.
You don't have to rely on the default output that a command gives you.
If you need something else, you can probably create it,
and it's really not that difficult.
You can do it all from a prompt.
You don't have to be a developer in order to extend PowerShell.
In fact,
you can create custom objects right from the PowerShell
prompt that you can then use in the PowerShell pipeline to
do other things that you want.
I love this idea about PowerShell being a tool that I can make
work the way that I need it to get my job done.
I'm not relying necessarily on someone else to do that.
For example, if I want to create a custom property,
and I'm going to have some demonstrations here in a moment,
we'll go into this in much more detail, but let me give you an example here.
One way to create custom properties is with Select‑Object.
In my example here, I'm giving all of the files under the data directory,
and then I'm selecting some key properties,
so we've already seen Select‑Object, so I'm saying,
hey, give me the Fullname, the Name, and the LastWriteTime.
After that part, some custom properties,
and the custom properties are defined with a hash table,
and we've talked about hash tables earlier in the course.
So the hash table that you use to create a custom
property has two predefined keys, Name and Expression.
The Name will be the Name of the custom property,
so I'm creating a custom property called Size,
and the Expression value will be a script block,
and we looked at script blocks also earlier in the course.
The script block will execute once, in this case,
for every object in the pipeline, and that's what $_ is showing.
So the Size property will have a value of the Length
property of every file object in the pipeline.
The Computername property will have a value of the environmental variable,
computername.
Now, here's really the fun part.
I can create something out of thin air, so I'm going to create,
because the purpose of my little command here is I want to
basically create a little audit trail and say,
what's the status of the files on a particular date,
so I'm going to create a new property called Audit,
and the value of that property will be Get‑Date,
and I'm formatting it as a string because this is all
going to be exported to a CSV file anyway.
PowerShell and the ability to create a custom property allows me to get
more information that's relevant to what I want to do.
And by the way, when I use these things with Select‑Object,
this is also going to create a custom object,
although here's another way that you can create a custom object.
So I'm going to run Get‑Process and Get‑Service and
save those things to our variables.
Now, I'm creating a hash table,
$h, and in $h, the keys and values will become an object, and the
way that I create that object, this is one way, is using the new
object cmdlet, use the PSObject TypeName, and then the property
can just be that hash table.
Any hash table I can turn into an actual PowerShell object, just like that.
When I get to the demonstration, I'll also show you how to use the pscustomobject shortcut.