So enough of me chatting about all of this.
Let's go back into PowerShell one more time,
and let me give you a demonstration of working with some common string,
date, and number techniques,
and I also want to try to pull a lot of material from this course
all together in some more practical examples.
Let's first look at working with numbers in PowerShell.
PowerShell includes some built‑in shortcuts,
some of these you have seen already in the course,
but let me go over them again.
You can do, for example, one KB.
If you don't remember how many bytes are in 1 KB,
these numeric shortcuts will give you a value in bytes.
So instead of having to type or try to figure out what's 3 KB,
I can just do 3 KB, or 1 MB, or 1 GB, or even 1 TB.
You can do math.
I can say take this number, which I may have gotten from a process,
or a file size, or from somewhere, say, what is this value in megabytes?
And there we go.
Using the megabytes shortcut, I can convert that value of bytes,
assuming it's bytes, into a value of megabytes.
You can also use the as operator, which we looked at all so early in the course,
and say, hey, take this value, I'm going to divide by 1 GB,
and treat it as an integer, which has the effect of rounding it,
so there we go, so that would be then 51 GB.
Now, PowerShell doesn't have any cmdlets to do math,
but there is a .NET class that you can use,
and you can access the methods of that class in
order to do mathematical operations.
This is a little different because we are going to access
the .NET Framework directly in PowerShell,
and we're going to access the math class,
which we're going to put in square brackets.
Now, this math class has a bunch of methods.
A lot of these methods are repeated.
There are different ways to use these,
so I'm going to group them on the Name property.
Here then is a list of all of the methods,
and on the right side,
you can see some of the different ways that you can
use these particular methods.
I'm going to show you some of them.
We're not going to get into all of them.
So let's look at, for example, the power method.
Some of these methods will have multiple ways that they can be used,
so I'm using the OverloadedDefinitions property on
the method in order to get this.
This tells me,
and this is the same information that you would see in the GetMember output.
This shows me that this method will write a double type of object,
which is just another type of number with a decimal point in it.
So I'm going to invoke the math operator, and that is the double colons here.
That is the invoking a static method, so I'm going to raise 2 to the third power,
and of course, that gives me 8.
Now, if I need to find out, say, the square root of 144,
and obviously, I can see that it is 12.
Now, one method that I think you will use a lot is the round method.
You see there are lots of ways to use this.
Primarily,
the way that I use it is I specify the value and then
the number of decimals that I want.
Here, let me show you.
So if I take a number, we'll save it to variable $i,
and I want to round that to 2 decimal places.
There we go.
It's really that easy.
And this rounding still gives me a number.
So if I were to do this with a group of objects and wanted a sort on its value,
it will sort properly because it is a number.
So let's use some of these techniques in PowerShell.
Let me do Get‑ChildItem on my scripts folder and get
all of the files that are ps*, and I'm going to measure them,
and I'm going to measure the Length property and get the sum,
and I'm going to save this result to $m.
So if I look at the Sum property of the measurement object,
so that is a value in bytes of all, basically,
all of my PowerShell files in the scripts folder.
I don't know about you,
but I'm not necessarily very good at looking at that number going,
oh, that's so and so kilobytes or megabytes.
So I can divide that Sum by 1 KB or divide it by 1 MB.
Okay, so I've got 13.3 MB worth of files.
I could say, let's go back to the kilobyte version and treat it as an integer,
so I'm rounding off those with decimal points.
Let's use the round method here and format that sum value
divided by 1 MB to 4 decimal places,
and that gives me a result that might be more meaningful.
Let's take this a step further.
I am going to create a hash table called $get,
and this hash table will be used to splat to Get‑ChildItem so all
of the keys match parameters for Get‑ChildItem,
and these are the values then that I will use.
So I'm going to search the scripts folder for all files recursively,
but exclude the list of files that you see there.
And now I'm going to run Get‑ChildItem, splatting my hash table.
Now because some of my files may not have extensions,
I'm going to filter them with Where‑Object,
I'm not using any operator here because if the object has an Extension property,
if there's a value there, it will implicitly be true.
So as long as whatever code in the script block for the Where‑Object is true,
PowerShell will write that object to the pipeline,
which is good.
Because now I want to take those file objects and group them on the Extension,
and I'm going to use a parameter here for Group‑Object called AsHashTable.
This is going to take the output of Group‑Object
and actually create a hash table,
not a group measurement object as we have used in the past.
And just to see what's going on here,
I'm going to Tee the results to a Variable $g.
So I'll be able to see the results as PowerShell does it,
and I'm going to save the results to $g because I'm then going to use $g to
do something else. So we'll give this a moment to finish.
Alright, there we go.
You can see all of the hash table.
So there's the Name, which is the extension, and then the value
are all the files that go with that extension.
Now, let's take $g, and I'm going to use the GetEnumerator method
that we looked at in the lesson on hash tables.
I want to enumerate this hash table and select a few properties.
So the Name property on the hash table,
I'm just going to rename and call it file type.
I'm using select object and using this hash table that we've seen before.
The count property I'm going to create, and the value will be the
count of the value property of the hash table.
So $_ is the current object in the pipeline, which is the enumerated element
from the hash table, value is an array of files, and I'm getting the count
property. That value will be used and displayed as count.
I'm then going to create another property called SizeKB, and here, I'm
going to have a nested expression where I'm going to take the value, so
this can be all the files, and I'm going to pipe them to measure objects
and get the sum of the length properties.
Then I can do math on that value, like we did earlier, and
divide that by 1 KB to 2 decimal points,
and then last, I'll sort the object on the SizeKB that I'm
creating in descending order, and then I just want to see the
first 10 objects that come through.
So try to visualize what PowerShell is doing here, and there is the output
then. I could take this output, I could save it to a file,
I could export it to CSV, lots of things that I could do with it
because I'm working with an object in the pipeline.
PowerShell had no command that was going to give me this information,
but I was able to create and get what I needed in
order to meet some business need.
Let's look at one more practical example here. I'm going to use
Get‑CIMInstance and get the Win32_OperatingSystem class, and I'm also
going to save the output to a variable, $os.
Then I want to display just the Caption,
the TotalVisibleMemorySize, and the free physical memory.
All right, so there's the result that I get.
Don't assume that all the values are always in bytes.
So,
the numbers you see there actually are in kilobytes, and the
only reason I know that is because when I first started doing
this years ago, my values were off.
So then I looked up the Win32_OperatingSystem. If you go to Microsoft,
look at the documentation, and it will tell you that these values are in
kilobytes. Now that I know that they are in kilobytes,
I can create more meaningful output, like this, and I'm just
going to reuse my saved variable here.
So I'm going to select the Caption,
create a custom property called TotalMemGB, so it's going to be the total
visible memory size divided by 1 MB because it's already in kilobytes, and
I'm going to treat this as an integer, and I'll do the same thing with the
memory. Although, this time, I'm going to round that free physical memory
to 2 decimal points.
Now I can see that this computer has 16 GB of memory and 14.67 of it is free.
PowerShell didn't have the tools to give me the information I wanted,
but I was able to tell PowerShell using its language, this is what I need, show it to me.