1
00:00:01,040 --> 00:00:05,200
Now, let's look at ways of manipulating and working with strings.

2
00:00:05,200 --> 00:00:10,130
Let's define a string, PowerShell, save as a $s.

3
00:00:10,130 --> 00:00:13,410
Let's pipe $s to Get‑Member.

4
00:00:13,410 --> 00:00:15,630
There are lots of methods here that you can see,

5
00:00:15,630 --> 00:00:17,540
and we're going to go through some of them,

6
00:00:17,540 --> 00:00:22,540
and one Property, Length, which shows me how long the string is.

7
00:00:22,540 --> 00:00:27,140
So if I look at that, I can see that PowerShell is 10 characters long.

8
00:00:27,140 --> 00:00:29,530
We looked at this in the lesson on arrays,

9
00:00:29,530 --> 00:00:30,240
but remember,

10
00:00:30,240 --> 00:00:38,220
strings can be treated as an array, so the first index of the string 0 is P,

11
00:00:38,220 --> 00:00:41,330
or if I want to, I can use the range operator and say,

12
00:00:41,330 --> 00:00:44,950
hey, get me elements 3 through 6, and there we go,

13
00:00:44,950 --> 00:00:47,630
and that's treated then as another array.

14
00:00:47,630 --> 00:00:51,360
You can take strings and make them say all lowercase,

15
00:00:51,360 --> 00:00:54,420
or you can make them all uppercase.

16
00:00:54,420 --> 00:00:55,750
When you invoke these methods,

17
00:00:55,750 --> 00:00:58,960
this does not change the original or underlying object,

18
00:00:58,960 --> 00:01:00,660
so $s hasn't changed.

19
00:01:00,660 --> 00:01:09,940
All I did was invoke a method, and PowerShell wrote a new result to the pipeline.

20
00:01:09,940 --> 00:01:11,180
Now, let's parse $s,

21
00:01:11,180 --> 00:01:17,440
and there's a substring method that can be used a number of ways.

22
00:01:17,440 --> 00:01:23,440
Let's take $s and get substrings starting at index 5.

23
00:01:23,440 --> 00:01:25,920
Because I did not specify a length,

24
00:01:25,920 --> 00:01:30,940
PowerShell just went and gave me the rest of the string starting at index 5.

25
00:01:30,940 --> 00:01:32,930
Although if I wanted to, I could start at,

26
00:01:32,930 --> 00:01:36,640
in this case, 0 and give me 5 characters.

27
00:01:36,640 --> 00:01:39,730
It's really not that difficult to try to parse strings out.

28
00:01:39,730 --> 00:01:42,350
Remember though, PowerShell is all about objects,

29
00:01:42,350 --> 00:01:44,400
and I don't want you to be focusing on, oh,

30
00:01:44,400 --> 00:01:47,340
how do I, I need to parse strings and parsing output.

31
00:01:47,340 --> 00:01:49,870
This is the kind of thing that you might do if you have to parse a

32
00:01:49,870 --> 00:01:54,070
log file, for example, or you need to tweak some output in some

33
00:01:54,070 --> 00:01:56,640
custom objects that you are creating.

34
00:01:56,640 --> 00:01:59,180
Don't try to look at a Powershell command,

35
00:01:59,180 --> 00:02:00,960
and look at the output, and try thinking,

36
00:02:00,960 --> 00:02:03,780
okay, how do I parse this as string output?

37
00:02:03,780 --> 00:02:05,670
Don't go down that route.

38
00:02:05,670 --> 00:02:08,640
So going back to this example,

39
00:02:08,640 --> 00:02:14,000
$s.Substring(5) is just writing another object to the pipeline,

40
00:02:14,000 --> 00:02:18,640
so I can say, hey, make that all uppercase.

41
00:02:18,640 --> 00:02:20,230
Let me show you something else with strings,

42
00:02:20,230 --> 00:02:22,940
and this is something that you will encounter.

43
00:02:22,940 --> 00:02:25,780
So I'm going to create $t, and I'm going to create the string Pluralsight,

44
00:02:25,780 --> 00:02:28,300
but I'm putting spaces around it.

45
00:02:28,300 --> 00:02:31,180
Because when you're working with data in PowerShell you

46
00:02:31,180 --> 00:02:33,920
may not always know where it comes from,

47
00:02:33,920 --> 00:02:38,940
and you may need to clean it up, and that's what we're going to do here.

48
00:02:38,940 --> 00:02:41,130
You can see the space before, and you really can't tell

49
00:02:41,130 --> 00:02:46,040
that there is whitespace after the string.

50
00:02:46,040 --> 00:02:49,090
I can look at the length property and see that it is 15.

51
00:02:49,090 --> 00:02:52,940
That includes the whitespace.

52
00:02:52,940 --> 00:02:57,260
PowerShell has a number of methods that you can use on strings.

53
00:02:57,260 --> 00:03:00,210
You may, for example, only want to trim the end,

54
00:03:00,210 --> 00:03:03,860
meaning, get rid of all the whitespace that is at the end of the string,

55
00:03:03,860 --> 00:03:07,350
and it's kind of hard to see in this example, TrimStart

56
00:03:07,350 --> 00:03:11,510
a little bit easier to see there, or you can use the Trim method,

57
00:03:11,510 --> 00:03:15,640
which will trim the entire object, both start and end.

58
00:03:15,640 --> 00:03:17,950
We'll save that to $r.

59
00:03:17,950 --> 00:03:19,320
Now that looks right.

60
00:03:19,320 --> 00:03:23,040
And if I look at the length property, I can see that it is 11,

61
00:03:23,040 --> 00:03:27,640
so that previous length, obviously, were all whitespace characters.

62
00:03:27,640 --> 00:03:29,640
And here's how you might use all of this.

63
00:03:29,640 --> 00:03:32,320
So I'm going to create a variable to a txt file,

64
00:03:32,320 --> 00:03:33,990
so my work folder,

65
00:03:33,990 --> 00:03:36,230
and you have this file as part of the course downloads

66
00:03:36,230 --> 00:03:37,700
so if you wanted to play with here.

67
00:03:37,700 --> 00:03:41,940
So I do Get‑Content and see what's in that file.

68
00:03:41,940 --> 00:03:44,340
So this is supposed to be a list of computer names.

69
00:03:44,340 --> 00:03:46,880
I just have a localhost several times,

70
00:03:46,880 --> 00:03:49,640
Offline for a computer that's not available,

71
00:03:49,640 --> 00:03:51,360
and again, localhost.

72
00:03:51,360 --> 00:03:51,990
And you can see,

73
00:03:51,990 --> 00:03:56,120
I've got some blank lines and some extra spaces around localhost,

74
00:03:56,120 --> 00:03:59,940
so I need to kind of clean up this file if I want to use it.

75
00:03:59,940 --> 00:04:03,140
If I were to try to do this, for example,

76
00:04:03,140 --> 00:04:08,860
Get‑Content $f and run Test‑Connection, Test‑Connection will then take

77
00:04:08,860 --> 00:04:13,440
the contents of $f and use it for the computername.

78
00:04:13,440 --> 00:04:15,570
The first one works because localhost has no spacing,

79
00:04:15,570 --> 00:04:19,750
then I get some blank lines, I'll get an error for Offline,

80
00:04:19,750 --> 00:04:24,650
and if you look at the messages there, you'll see Testing connection to,

81
00:04:24,650 --> 00:04:27,200
and then in quotes, it's got localhost,

82
00:04:27,200 --> 00:04:30,440
and you can actually see that there are spaces around that.

83
00:04:30,440 --> 00:04:31,790
Well, actually, I don't want that.

84
00:04:31,790 --> 00:04:36,740
So I want to be able to clean this up, and here's how we can do that.

85
00:04:36,740 --> 00:04:40,540
So we'll do Get‑Content.

86
00:04:40,540 --> 00:04:40,740
Now,

87
00:04:40,740 --> 00:04:43,390
for each string that comes through from

88
00:04:43,390 --> 00:04:47,660
Get‑Content, now this is one way to do it,

89
00:04:47,660 --> 00:04:49,900
and I'm going to use, you know,

90
00:04:49,900 --> 00:04:53,430
some new PowerShell 7 syntax we looked at earlier in the course.

91
00:04:53,430 --> 00:04:59,130
If the length is greater than 0, then I'm going to run Test‑Connection,

92
00:04:59,130 --> 00:05:02,240
and for the computername, use the current object,

93
00:05:02,240 --> 00:05:06,260
but Trim all of the whitespace, both front and back,

94
00:05:06,260 --> 00:05:10,760
on that; otherwise, that's the single colon there,

95
00:05:10,760 --> 00:05:17,840
otherwise, I'm going to Write‑Warning "Skipping blank".

96
00:05:17,840 --> 00:05:18,850
So there we go.

97
00:05:18,850 --> 00:05:21,630
I can see that I get my results for localhosts,

98
00:05:21,630 --> 00:05:24,060
I get skipping the blank lines,

99
00:05:24,060 --> 00:05:26,480
and I get the error message for the computer that is Offline,

100
00:05:26,480 --> 00:05:28,460
which is perfectly fine.

101
00:05:28,460 --> 00:05:31,940
From a more practical perspective, this might be a better approach though.

102
00:05:31,940 --> 00:05:48,740
Let's do Get‑Content again, get $f, where the length is greater than 0.

103
00:05:48,740 --> 00:05:50,310
And then for each result,

104
00:05:50,310 --> 00:05:54,050
I want to take the $_ against the current object in the pipeline,

105
00:05:54,050 --> 00:05:59,140
so that string that comes through that has a length greater than 0,

106
00:05:59,140 --> 00:06:04,440
trim it and use that as a computername for Test‑Connection.

107
00:06:04,440 --> 00:06:08,840
Now, I don't worry about the blanks because those really don't matter to me.

108
00:06:08,840 --> 00:06:11,200
I get the computers that work, localhosts,

109
00:06:11,200 --> 00:06:13,420
and I get my error message for Offline.

110
00:06:13,420 --> 00:06:17,100
That's probably an easier, cleaner way to do this.

111
00:06:17,100 --> 00:06:17,280
Now,

112
00:06:17,280 --> 00:06:20,790
the reason that I did not do Get‑Content pipe to Where object pipe to

113
00:06:20,790 --> 00:06:25,290
Test‑Connection is because I needed to invoke that Trim method on

114
00:06:25,290 --> 00:06:28,040
every object that came through the pipeline.

115
00:06:28,040 --> 00:06:34,240
So I needed to use for each object in order to achieve that.

116
00:06:34,240 --> 00:06:35,070
All right, next,

117
00:06:35,070 --> 00:06:38,980
I want to show you about other things that we can do with strings.

118
00:06:38,980 --> 00:06:43,840
So remember, we've got $s here, so it's PowerShell.

119
00:06:43,840 --> 00:06:48,240
Strings also have a replace method.

120
00:06:48,240 --> 00:06:49,940
Different ways that we can use it.

121
00:06:49,940 --> 00:06:54,740
So I can say replace all the es with the number 3.

122
00:06:54,740 --> 00:06:56,770
Again, this doesn't change $s.

123
00:06:56,770 --> 00:07:00,940
It just writes a new string to the pipeline.

124
00:07:00,940 --> 00:07:05,040
There is a replace method that we can use.

125
00:07:05,040 --> 00:07:08,940
And one thing to notice about this method, it is case sensitive.

126
00:07:08,940 --> 00:07:14,740
So if I try to replace the lowercase p with X, it doesn't work.

127
00:07:14,740 --> 00:07:17,740
So I would have to do something like this,

128
00:07:17,740 --> 00:07:22,390
ignore the casing of the string, and just replace any P,

129
00:07:22,390 --> 00:07:25,240
capital case or lowercase with X.

130
00:07:25,240 --> 00:07:25,780
Remember,

131
00:07:25,780 --> 00:07:29,570
you will often be working with data and values from commands that you

132
00:07:29,570 --> 00:07:32,430
have no way of knowing what is in that variable,

133
00:07:32,430 --> 00:07:35,070
and so you may need to take certain things into account,

134
00:07:35,070 --> 00:07:41,840
and this is one example.

135
00:07:41,840 --> 00:07:43,340
So let me do this.

136
00:07:43,340 --> 00:07:45,250
All right, see if you can follow this.

137
00:07:45,250 --> 00:07:49,540
Remember we talked about the importance of leveraging the object notation.

138
00:07:49,540 --> 00:07:56,240
So I'm going to take $s, and I'm going to replace o with the set of parentheses.

139
00:07:56,240 --> 00:07:59,140
Remember, that's writing a new object of the pipeline.

140
00:07:59,140 --> 00:08:01,300
I'm then going to replace that object,

141
00:08:01,300 --> 00:08:08,240
replace the e with the greater than sign and then replace h with the &.

142
00:08:08,240 --> 00:08:11,840
In other words, I'm going to make three replacements all at once,

143
00:08:11,840 --> 00:08:13,240
and there we go.

144
00:08:13,240 --> 00:08:13,610
Now,

145
00:08:13,610 --> 00:08:17,870
another option though for replacing if we don't want to worry too much

146
00:08:17,870 --> 00:08:22,140
about case sensitivity is use the replace operator.

147
00:08:22,140 --> 00:08:28,020
So now, I can take $s and say replace P with the!, and that will work.

148
00:08:28,020 --> 00:08:33,620
So the replace method is case sensitive unless you use that ordinal ignore case,

149
00:08:33,620 --> 00:08:39,840
or you can use to replace method, which is not case sensitive.

150
00:08:39,840 --> 00:08:42,760
The other reason that you might want to use to replace method

151
00:08:42,760 --> 00:08:46,140
is that you can use regular expressions.

152
00:08:46,140 --> 00:08:49,860
So I'm going to replace, in the string jeff789,

153
00:08:49,860 --> 00:08:52,920
I'm going to replace any number with the letter X,

154
00:08:52,920 --> 00:08:57,340
and there we go.

155
00:08:57,340 --> 00:08:58,300
So with that in mind,

156
00:08:58,300 --> 00:09:01,470
let's look at some ways that we can take advantage

157
00:09:01,470 --> 00:09:04,640
of these operators and methods, and we create a string,

158
00:09:04,640 --> 00:09:09,840
$data, and this might be data that you get from a log file.

159
00:09:09,840 --> 00:09:14,820
Now, I can split that string using the split method into an array.

160
00:09:14,820 --> 00:09:19,720
And because that is an array of object, and we saw this in the array module,

161
00:09:19,720 --> 00:09:21,150
I can invoke a method,

162
00:09:21,150 --> 00:09:24,900
and PowerShell will invoke this method on all members of the array.

163
00:09:24,900 --> 00:09:29,960
So I can take the data, split it on the pipe,

164
00:09:29,960 --> 00:09:33,540
or the vertical bar, that's going to leave some spaces,

165
00:09:33,540 --> 00:09:36,840
but no problem because I'm going to trim it.

166
00:09:36,840 --> 00:09:39,330
So if I look at my array, you can see, well,

167
00:09:39,330 --> 00:09:41,050
it may be kind of hard to see, but you see,

168
00:09:41,050 --> 00:09:45,520
I got rid of, for example, the leading space in front of deco,

169
00:09:45,520 --> 00:09:48,940
so that's very useful.

170
00:09:48,940 --> 00:09:51,440
See if you can follow what I'm doing here though.

171
00:09:51,440 --> 00:09:57,640
Remember, $arr is an array, element 0 is the first element in the array,

172
00:09:57,640 --> 00:10:01,710
art, art is a string, strings can be arrays,

173
00:10:01,710 --> 00:10:06,040
so element 0 in art is letter a, right,

174
00:10:06,040 --> 00:10:09,040
so let's see if you can follow along here.

175
00:10:09,040 --> 00:10:12,940
I'm going to initialize an empty array, $name,

176
00:10:12,940 --> 00:10:17,540
and I'm going to add to $name this command.

177
00:10:17,540 --> 00:10:21,940
Let me type it out here, and let me explain then what we're doing.

178
00:10:21,940 --> 00:10:27,740
All right, So I'm going to take the first element in the array $arr,

179
00:10:27,740 --> 00:10:33,240
which is art, right, and I'm going to use the replace method.

180
00:10:33,240 --> 00:10:33,430
Now,

181
00:10:33,430 --> 00:10:37,720
I'm going to replace the first element in the array

182
00:10:37,720 --> 00:10:40,050
and the first letter in the array, right,

183
00:10:40,050 --> 00:10:41,770
a, the lowercase a,

184
00:10:41,770 --> 00:10:49,240
and I'm going to replace that with basically the same character $arr[0][0].

185
00:10:49,240 --> 00:10:52,740
Now, that is going to actually technically be a character object,

186
00:10:52,740 --> 00:10:56,100
and so in order to use the ToUpper method,

187
00:10:56,100 --> 00:10:58,440
I need to turn it to a string.

188
00:10:58,440 --> 00:11:03,040
So I'm taking the first character in art,

189
00:11:03,040 --> 00:11:08,340
turning it back into a string, and then invoking the ToUpper method.

190
00:11:08,340 --> 00:11:11,100
This seems more complicated than it is,

191
00:11:11,100 --> 00:11:14,940
but that's only because I'm trying to explain to you what is happening.

192
00:11:14,940 --> 00:11:21,040
Eventually, this all becomes second nature, and you won't even think about it.

193
00:11:21,040 --> 00:11:26,540
And then I'll do the same thing with the last name.

194
00:11:26,540 --> 00:11:31,710
So the second element in the array $arr is deco.

195
00:11:31,710 --> 00:11:34,940
And, again, I'm going to replace the first letter,

196
00:11:34,940 --> 00:11:39,740
which is lowercase, with the first letter toString and make it uppercase.

197
00:11:39,740 --> 00:11:46,260
So now, $name will have the first name and last name, all in proper case.

198
00:11:46,260 --> 00:11:48,030
There are other ways that you could do this,

199
00:11:48,030 --> 00:11:53,430
but I wanted to use this example to demonstrate these methods and how

200
00:11:53,430 --> 00:11:58,230
you can use PowerShell to string things together. Once I have this, and

201
00:11:58,230 --> 00:12:00,570
maybe this is part of a script or something that I'm building to

202
00:12:00,570 --> 00:12:05,860
provision a user account, I could then display a message to the user,

203
00:12:05,860 --> 00:12:12,350
writing Creating user, and I'm going to take that name array and join

204
00:12:12,350 --> 00:12:14,540
it with spaces.

205
00:12:14,540 --> 00:12:15,370
So now, I can say, hey,

206
00:12:15,370 --> 00:12:17,880
I'm creating user art deco and feed that back to the

207
00:12:17,880 --> 00:12:20,720
user so they know what is going on.

208
00:12:20,720 --> 00:12:23,270
There are no cmdlets to do any of this.

209
00:12:23,270 --> 00:12:25,050
There are methods and operators,

210
00:12:25,050 --> 00:12:33,000
and I'm just using the PowerShell language to give me information that I need in order to do my job.

