- I'm gonna start by building a typical script.
But I'm gonna show you how to advance this
into something better than just a regular script,
turning this into a useful tool.
First of all, I like to solve my problem at the console.
What I'm gonna use is something
from what's called a WMI and CIM.
I'm gonna use a command called CimInstance.
This gives me access to a series of classes
that can get information for me.
I'm gonna grab some disk information,
logicaldisk,
and I would like to grab this.
If I run it right now,
you can see I'm getting my drive information.
I can pipe this to get-member.
There's a bunch of properties I'm actually receiving
about my disk.
But I wanna filter this.
This is filtering to the left.
I wanna filter on DeviceID=
just the C drive for right now.
So I'm getting my C drive information.
I wanna get it from a different computer.
So I'm gonna use ComputerName DC.
That's coming from my domain controller
rather than my local machine.
I don't want all of these properties.
So in my brain, I'm, like,
"Okay, I'm getting it from the right machine,
"but now I just wanna select the ...
"Oh, I don't ... I forgot what properties."
This is when I'll back up,
I'll pipe it to get-member,
and I'll go out and I'll find the properties that I want.
Actually, the properties that I want for this
are FreeSpace ...
There's FreeSpace ...
I wanna find the free space on that machine.
Also, I need to make sure
I'm keeping track of the machine's name.
So there is a property in here
for PSComputerName.
Those are the two properties I want.
I will Select-Object -Property.
I'm gonna throw them in an alias here.
That way, you can see how I normally do this.
I'm usually typing at the console
with lots of aliases.
Select PSComputerName,
FreeSpace.
Now, that's not a bad kind of solution right there.
I mean, I've got the computer name,
I've got the free space.
There's a couple of things I don't like about this.
I don't like the fact
that it's called PSComputerName as a column,
and I don't like that Freespace number.
Instead, what I think I would prefer to do
is change those to custom columns
and a calculated property.
Here's what I'm gonna do.
I'm actually gonna let these stay there
and I'm gonna use them as I build the syntax.
The special syntax for the custom column
was an @ sign,
and an open and a closed squiggly.
I have two parts to it,
n= the name of the column.
And I want to call this ComputerName.
That makes much more sense.
The expression that I'm gonna fill it with
is e=,
and this will be the current object
coming across the pipeline,
.PSComputerName.
See, that's why I left that PSComputerName there,
was so I didn't forget what the property name was called,
which happens a lot.
So, little tip.
Now, I still have one more that I wanna do,
and that's this FreeSpace.
I'm gonna make this number look better.
I'm gonna, again, do that same thing.
I'll put in n=,
and we'll call this
FreeGB,
because we're gonna do it in gigabytes.
I'll do e=
the current object coming across the pipeline,
which is represented $_.
and it's FreeSpace.
When you're doing a lot of properties,
it's easy to forget them.
So that's why I do that.
Now, if I run this right now,
I like to check to make sure everything's working.
Yeah, it's working great, working great.
Still don't like that number.
Now I can fix that number.
Divide by 1gb,
and I want this as an integer.
Now, that's looking pretty good.
I wanna turn this into a script
so I never have to type this again.
Like we did before,
I'm gonna copy this.
Only, instead of using Notepad,
I'm gonna use the ISE.
Let me show you how I get this into the ISE
and clean it up.
I'm gonna maximize the screen here,
I'll flip to ...
Let me get rid of him.
I'm gonna increase my font size,
take him back up to 160.
I'm gonna go to a single pane,
so I can show this to you.
I'm gonna paste this in.
Now, I have some cleanup work to do,
but the color syntaxing is gonna help me make this easier.
I wanna show you a couple of tips and tricks
in making this something that's a lot easier to read.
Now, right now, I want you to take a look
at what I'm having to do to look at all of this.
See, I have to scroll
back and forth across here.
I don't like that.
What I'm gonna do is, I'm gonna save this
and then show you where we can put in
some line continuation.
I'm gonna go out to my Scripts folder.
I'll save this as ...
Let's call DiskInfo.psq1.
Line continuation in a script
can be done a couple of different ways.
One of the places where you can do line continuation
is at this pipeline character.
Watch. I'll hit Enter,
and I'll tab ...
See how the color syntaxing still works?
In other words, you can break lines apart
although the system still reads them as one line
at the pipeline character.
Another place that you can do this,
and there are several,
but another place you can do this is at commas.
I got Select ...
Oh, I could do it right here, at a comma.
This helps me kind of keep things lined up
and looking prettier.
So I could actually move this out
and keep things aligned.
Now, one of the things we always wanna do when we script is,
make sure we don't have any aliases
and we're always using the parameters
that go along with their arguments.
Take a look. This Select is not correct.
This should be Select-Object,
and it should be -property.
Ah, that's better.
Now I've got ComputerName, I've got this.
It's easier to see, it's more on the screen.
But not everything is completely on the screen.
There is an official line continuation character
that you can use,
like, if I wanted to break the line here.
The only challenge is,
is that it's very difficult to read
if you were to print this out
or if you were to see it on the Internet.
It's a back tick.
I could break the line here and still use it.
But the back tick, I try to avoid it,
but sometimes you just can't avoid using the back tick.
In this case, I am gonna avoid it.
Now, for us to continue on in this demonstration
of turning this into a great script
and to a great tool,
what I'm gonna do is,
I'm gonna rearrange this just a little bit,
and I'm gonna put the ComputerName up here first.
I like to verify that this still works, so I'll run it.
Tells me it'll be saved, that's fine.
Yep, it still works.
So everything is still good.
Now, this is the start to our script.
But this is not the end of our script.
Some people stop right here.
Okay, I've got it scripted.
But now I want to turn this into a more usable tool,
and I wanna get you started on how to do that.