1
00:00:02,940 --> 00:00:05,550
So enough of me chatting about all of this.

2
00:00:05,550 --> 00:00:07,570
Let's go back into PowerShell one more time,

3
00:00:07,570 --> 00:00:11,310
and let me give you a demonstration of working with some common string,

4
00:00:11,310 --> 00:00:12,930
date, and number techniques,

5
00:00:12,930 --> 00:00:16,010
and I also want to try to pull a lot of material from this course

6
00:00:16,010 --> 00:00:19,600
all together in some more practical examples.

7
00:00:19,600 --> 00:00:24,340
Let's first look at working with numbers in PowerShell.

8
00:00:24,340 --> 00:00:26,520
PowerShell includes some built‑in shortcuts,

9
00:00:26,520 --> 00:00:29,210
some of these you have seen already in the course,

10
00:00:29,210 --> 00:00:31,640
but let me go over them again.

11
00:00:31,640 --> 00:00:33,930
You can do, for example, one KB.

12
00:00:33,930 --> 00:00:36,830
If you don't remember how many bytes are in 1 KB,

13
00:00:36,830 --> 00:00:41,090
these numeric shortcuts will give you a value in bytes.

14
00:00:41,090 --> 00:00:44,400
So instead of having to type or try to figure out what's 3 KB,

15
00:00:44,400 --> 00:00:54,740
I can just do 3 KB, or 1 MB, or 1 GB, or even 1 TB.

16
00:00:54,740 --> 00:00:55,500
You can do math.

17
00:00:55,500 --> 00:00:59,020
I can say take this number, which I may have gotten from a process,

18
00:00:59,020 --> 00:01:03,940
or a file size, or from somewhere, say, what is this value in megabytes?

19
00:01:03,940 --> 00:01:04,570
And there we go.

20
00:01:04,570 --> 00:01:08,990
Using the megabytes shortcut, I can convert that value of bytes,

21
00:01:08,990 --> 00:01:14,040
assuming it's bytes, into a value of megabytes.

22
00:01:14,040 --> 00:01:18,390
You can also use the as operator, which we looked at all so early in the course,

23
00:01:18,390 --> 00:01:22,210
and say, hey, take this value, I'm going to divide by 1 GB,

24
00:01:22,210 --> 00:01:26,240
and treat it as an integer, which has the effect of rounding it,

25
00:01:26,240 --> 00:01:31,680
so there we go, so that would be then 51 GB.

26
00:01:31,680 --> 00:01:35,720
Now, PowerShell doesn't have any cmdlets to do math,

27
00:01:35,720 --> 00:01:38,880
but there is a .NET class that you can use,

28
00:01:38,880 --> 00:01:42,400
and you can access the methods of that class in

29
00:01:42,400 --> 00:01:45,620
order to do mathematical operations.

30
00:01:45,620 --> 00:01:48,320
This is a little different because we are going to access

31
00:01:48,320 --> 00:01:51,540
the .NET Framework directly in PowerShell,

32
00:01:51,540 --> 00:01:54,150
and we're going to access the math class,

33
00:01:54,150 --> 00:01:56,940
which we're going to put in square brackets.

34
00:01:56,940 --> 00:02:00,640
Now, this math class has a bunch of methods.

35
00:02:00,640 --> 00:02:02,140
A lot of these methods are repeated.

36
00:02:02,140 --> 00:02:03,040
There are different ways to use these,

37
00:02:03,040 --> 00:02:05,920
so I'm going to group them on the Name property.

38
00:02:05,920 --> 00:02:08,680
Here then is a list of all of the methods,

39
00:02:08,680 --> 00:02:09,870
and on the right side,

40
00:02:09,870 --> 00:02:12,620
you can see some of the different ways that you can

41
00:02:12,620 --> 00:02:14,860
use these particular methods.

42
00:02:14,860 --> 00:02:15,750
I'm going to show you some of them.

43
00:02:15,750 --> 00:02:19,140
We're not going to get into all of them.

44
00:02:19,140 --> 00:02:22,180
So let's look at, for example, the power method.

45
00:02:22,180 --> 00:02:25,370
Some of these methods will have multiple ways that they can be used,

46
00:02:25,370 --> 00:02:28,180
so I'm using the OverloadedDefinitions property on

47
00:02:28,180 --> 00:02:30,880
the method in order to get this.

48
00:02:30,880 --> 00:02:32,210
This tells me,

49
00:02:32,210 --> 00:02:37,180
and this is the same information that you would see in the GetMember output.

50
00:02:37,180 --> 00:02:41,460
This shows me that this method will write a double type of object,

51
00:02:41,460 --> 00:02:46,940
which is just another type of number with a decimal point in it.

52
00:02:46,940 --> 00:02:51,520
So I'm going to invoke the math operator, and that is the double colons here.

53
00:02:51,520 --> 00:02:57,390
That is the invoking a static method, so I'm going to raise 2 to the third power,

54
00:02:57,390 --> 00:02:59,420
and of course, that gives me 8.

55
00:02:59,420 --> 00:03:03,400
Now, if I need to find out, say, the square root of 144,

56
00:03:03,400 --> 00:03:07,440
and obviously, I can see that it is 12.

57
00:03:07,440 --> 00:03:13,540
Now, one method that I think you will use a lot is the round method.

58
00:03:13,540 --> 00:03:16,430
You see there are lots of ways to use this.

59
00:03:16,430 --> 00:03:17,200
Primarily,

60
00:03:17,200 --> 00:03:21,450
the way that I use it is I specify the value and then

61
00:03:21,450 --> 00:03:23,000
the number of decimals that I want.

62
00:03:23,000 --> 00:03:25,340
Here, let me show you.

63
00:03:25,340 --> 00:03:28,130
So if I take a number, we'll save it to variable $i,

64
00:03:28,130 --> 00:03:34,440
and I want to round that to 2 decimal places.

65
00:03:34,440 --> 00:03:35,170
There we go.

66
00:03:35,170 --> 00:03:36,570
It's really that easy.

67
00:03:36,570 --> 00:03:38,670
And this rounding still gives me a number.

68
00:03:38,670 --> 00:03:42,630
So if I were to do this with a group of objects and wanted a sort on its value,

69
00:03:42,630 --> 00:03:46,080
it will sort properly because it is a number.

70
00:03:46,080 --> 00:03:50,040
So let's use some of these techniques in PowerShell.

71
00:03:50,040 --> 00:03:53,000
Let me do Get‑ChildItem on my scripts folder and get

72
00:03:53,000 --> 00:03:58,840
all of the files that are ps*, and I'm going to measure them,

73
00:03:58,840 --> 00:04:01,060
and I'm going to measure the Length property and get the sum,

74
00:04:01,060 --> 00:04:04,540
and I'm going to save this result to $m.

75
00:04:04,540 --> 00:04:08,040
So if I look at the Sum property of the measurement object,

76
00:04:08,040 --> 00:04:10,180
so that is a value in bytes of all, basically,

77
00:04:10,180 --> 00:04:13,050
all of my PowerShell files in the scripts folder.

78
00:04:13,050 --> 00:04:14,100
I don't know about you,

79
00:04:14,100 --> 00:04:16,630
but I'm not necessarily very good at looking at that number going,

80
00:04:16,630 --> 00:04:19,590
oh, that's so and so kilobytes or megabytes.

81
00:04:19,590 --> 00:04:25,640
So I can divide that Sum by 1 KB or divide it by 1 MB.

82
00:04:25,640 --> 00:04:31,540
Okay, so I've got 13.3 MB worth of files.

83
00:04:31,540 --> 00:04:35,320
I could say, let's go back to the kilobyte version and treat it as an integer,

84
00:04:35,320 --> 00:04:40,640
so I'm rounding off those with decimal points.

85
00:04:40,640 --> 00:04:43,940
Let's use the round method here and format that sum value

86
00:04:43,940 --> 00:04:47,340
divided by 1 MB to 4 decimal places,

87
00:04:47,340 --> 00:04:51,340
and that gives me a result that might be more meaningful.

88
00:04:51,340 --> 00:04:53,340
Let's take this a step further.

89
00:04:53,340 --> 00:05:00,140
I am going to create a hash table called $get,

90
00:05:00,140 --> 00:05:04,300
and this hash table will be used to splat to Get‑ChildItem so all

91
00:05:04,300 --> 00:05:08,040
of the keys match parameters for Get‑ChildItem,

92
00:05:08,040 --> 00:05:10,160
and these are the values then that I will use.

93
00:05:10,160 --> 00:05:14,520
So I'm going to search the scripts folder for all files recursively,

94
00:05:14,520 --> 00:05:19,860
but exclude the list of files that you see there.

95
00:05:19,860 --> 00:05:28,140
And now I'm going to run Get‑ChildItem, splatting my hash table.

96
00:05:28,140 --> 00:05:34,040
Now because some of my files may not have extensions,

97
00:05:34,040 --> 00:05:37,140
I'm going to filter them with Where‑Object,

98
00:05:37,140 --> 00:05:43,740
I'm not using any operator here because if the object has an Extension property,

99
00:05:43,740 --> 00:05:48,240
if there's a value there, it will implicitly be true.

100
00:05:48,240 --> 00:05:53,210
So as long as whatever code in the script block for the Where‑Object is true,

101
00:05:53,210 --> 00:05:55,590
PowerShell will write that object to the pipeline,

102
00:05:55,590 --> 00:05:56,410
which is good.

103
00:05:56,410 --> 00:06:03,670
Because now I want to take those file objects and group them on the Extension,

104
00:06:03,670 --> 00:06:07,590
and I'm going to use a parameter here for Group‑Object called AsHashTable.

105
00:06:07,590 --> 00:06:09,420
This is going to take the output of Group‑Object

106
00:06:09,420 --> 00:06:11,180
and actually create a hash table,

107
00:06:11,180 --> 00:06:14,940
not a group measurement object as we have used in the past.

108
00:06:14,940 --> 00:06:16,370
And just to see what's going on here,

109
00:06:16,370 --> 00:06:20,240
I'm going to Tee the results to a Variable $g.

110
00:06:20,240 --> 00:06:23,820
So I'll be able to see the results as PowerShell does it,

111
00:06:23,820 --> 00:06:29,370
and I'm going to save the results to $g because I'm then going to use $g to

112
00:06:29,370 --> 00:06:34,640
do something else. So we'll give this a moment to finish.

113
00:06:34,640 --> 00:06:35,480
Alright, there we go.

114
00:06:35,480 --> 00:06:38,560
You can see all of the hash table.

115
00:06:38,560 --> 00:06:41,750
So there's the Name, which is the extension, and then the value

116
00:06:41,750 --> 00:06:45,840
are all the files that go with that extension.

117
00:06:45,840 --> 00:06:53,070
Now, let's take $g, and I'm going to use the GetEnumerator method

118
00:06:53,070 --> 00:06:56,140
that we looked at in the lesson on hash tables.

119
00:06:56,140 --> 00:07:02,240
I want to enumerate this hash table and select a few properties.

120
00:07:02,240 --> 00:07:04,770
So the Name property on the hash table,

121
00:07:04,770 --> 00:07:07,820
I'm just going to rename and call it file type.

122
00:07:07,820 --> 00:07:14,240
I'm using select object and using this hash table that we've seen before.

123
00:07:14,240 --> 00:07:18,920
The count property I'm going to create, and the value will be the

124
00:07:18,920 --> 00:07:22,340
count of the value property of the hash table.

125
00:07:22,340 --> 00:07:28,670
So $_ is the current object in the pipeline, which is the enumerated element

126
00:07:28,670 --> 00:07:34,600
from the hash table, value is an array of files, and I'm getting the count

127
00:07:34,600 --> 00:07:40,840
property. That value will be used and displayed as count.

128
00:07:40,840 --> 00:07:46,410
I'm then going to create another property called SizeKB, and here, I'm

129
00:07:46,410 --> 00:07:50,940
going to have a nested expression where I'm going to take the value, so

130
00:07:50,940 --> 00:07:57,090
this can be all the files, and I'm going to pipe them to measure objects

131
00:07:57,090 --> 00:08:00,740
and get the sum of the length properties.

132
00:08:00,740 --> 00:08:05,250
Then I can do math on that value, like we did earlier, and

133
00:08:05,250 --> 00:08:10,740
divide that by 1 KB to 2 decimal points,

134
00:08:10,740 --> 00:08:15,950
and then last, I'll sort the object on the SizeKB that I'm

135
00:08:15,950 --> 00:08:20,410
creating in descending order, and then I just want to see the

136
00:08:20,410 --> 00:08:23,040
first 10 objects that come through.

137
00:08:23,040 --> 00:08:28,030
So try to visualize what PowerShell is doing here, and there is the output

138
00:08:28,030 --> 00:08:31,690
then. I could take this output, I could save it to a file,

139
00:08:31,690 --> 00:08:34,880
I could export it to CSV, lots of things that I could do with it

140
00:08:34,880 --> 00:08:38,040
because I'm working with an object in the pipeline.

141
00:08:38,040 --> 00:08:41,500
PowerShell had no command that was going to give me this information,

142
00:08:41,500 --> 00:08:44,810
but I was able to create and get what I needed in

143
00:08:44,810 --> 00:08:48,140
order to meet some business need.

144
00:08:48,140 --> 00:08:51,910
Let's look at one more practical example here. I'm going to use

145
00:08:51,910 --> 00:08:56,120
Get‑CIMInstance and get the Win32_OperatingSystem class, and I'm also

146
00:08:56,120 --> 00:09:01,640
going to save the output to a variable, $os.

147
00:09:01,640 --> 00:09:03,600
Then I want to display just the Caption,

148
00:09:03,600 --> 00:09:08,340
the TotalVisibleMemorySize, and the free physical memory.

149
00:09:08,340 --> 00:09:11,040
All right, so there's the result that I get.

150
00:09:11,040 --> 00:09:14,430
Don't assume that all the values are always in bytes.

151
00:09:14,430 --> 00:09:14,660
So,

152
00:09:14,660 --> 00:09:18,330
the numbers you see there actually are in kilobytes, and the

153
00:09:18,330 --> 00:09:22,400
only reason I know that is because when I first started doing

154
00:09:22,400 --> 00:09:24,810
this years ago, my values were off.

155
00:09:24,810 --> 00:09:29,870
So then I looked up the Win32_OperatingSystem. If you go to Microsoft,

156
00:09:29,870 --> 00:09:33,680
look at the documentation, and it will tell you that these values are in

157
00:09:33,680 --> 00:09:37,220
kilobytes. Now that I know that they are in kilobytes,

158
00:09:37,220 --> 00:09:42,100
I can create more meaningful output, like this, and I'm just

159
00:09:42,100 --> 00:09:45,740
going to reuse my saved variable here.

160
00:09:45,740 --> 00:09:47,740
So I'm going to select the Caption,

161
00:09:47,740 --> 00:09:51,870
create a custom property called TotalMemGB, so it's going to be the total

162
00:09:51,870 --> 00:09:57,040
visible memory size divided by 1 MB because it's already in kilobytes, and

163
00:09:57,040 --> 00:10:02,070
I'm going to treat this as an integer, and I'll do the same thing with the

164
00:10:02,070 --> 00:10:07,080
memory. Although, this time, I'm going to round that free physical memory

165
00:10:07,080 --> 00:10:10,040
to 2 decimal points.

166
00:10:10,040 --> 00:10:17,440
Now I can see that this computer has 16 GB of memory and 14.67 of it is free.

167
00:10:17,440 --> 00:10:20,320
PowerShell didn't have the tools to give me the information I wanted,

168
00:10:20,320 --> 00:10:28,000
but I was able to tell PowerShell using its language, this is what I need, show it to me.

