1
00:00:00,240 --> 00:00:02,790
So now that we've looked at formatting and casting,

2
00:00:02,790 --> 00:00:06,210
one of the things that we do is actually manipulate data,

3
00:00:06,210 --> 00:00:10,840
and we're going to focus on string data, so we're talking text values.

4
00:00:10,840 --> 00:00:12,920
So common string manipulation,

5
00:00:12,920 --> 00:00:16,970
normally the most common one is replacing string values.

6
00:00:16,970 --> 00:00:22,170
So you may start with, let's say, a sentence or a few characters,

7
00:00:22,170 --> 00:00:26,140
and you wish to replace something in that value.

8
00:00:26,140 --> 00:00:28,300
Next would be splitting string values.

9
00:00:28,300 --> 00:00:31,030
This is very, very common. You know, as an example,

10
00:00:31,030 --> 00:00:34,380
if you're managing something on a network and you

11
00:00:34,380 --> 00:00:36,310
need to retrieve domain accounts,

12
00:00:36,310 --> 00:00:40,330
often the biggest thing is splitting out the domain

13
00:00:40,330 --> 00:00:42,930
and the dash and the user account,

14
00:00:42,930 --> 00:00:46,740
or maybe it's an email format you want to split out.

15
00:00:46,740 --> 00:00:49,540
Then, of course, you've got padding string values.

16
00:00:49,540 --> 00:00:51,100
This one is less used,

17
00:00:51,100 --> 00:00:56,540
but this allows you to pad values at either end of a string value.

18
00:00:56,540 --> 00:01:00,040
So the first thing we have is the ‑replace operator.

19
00:01:00,040 --> 00:01:05,200
So this takes an input string, then the replace operator,

20
00:01:05,200 --> 00:01:09,720
then there's a matching pattern, and then there's the string replacement.

21
00:01:09,720 --> 00:01:13,910
So that's kind of the four key pieces of the replace operator.

22
00:01:13,910 --> 00:01:17,540
So let's look at how we use the replace operator.

23
00:01:17,540 --> 00:01:19,280
So first off, we have two variables.

24
00:01:19,280 --> 00:01:21,580
variable1, which is a sentence,

25
00:01:21,580 --> 00:01:24,610
The class instructor asked for a volunteer for a demonstration,

26
00:01:24,610 --> 00:01:28,670
and then variable2 is a name, so Jones, Tom,

27
00:01:28,670 --> 00:01:30,320
Tom Jones, whatever it would be.

28
00:01:30,320 --> 00:01:32,660
Now if we're using the replace operator,

29
00:01:32,660 --> 00:01:38,740
we can take variable1, and we can replace the value inside of it.

30
00:01:38,740 --> 00:01:41,240
Now think carefully about this one.

31
00:01:41,240 --> 00:01:44,130
Variable1 is the containing object.

32
00:01:44,130 --> 00:01:47,760
If I then say variable1 ‑replace instructor,

33
00:01:47,760 --> 00:01:50,570
teacher, the thing you have to remember is,

34
00:01:50,570 --> 00:01:52,680
is that going to update variable1,

35
00:01:52,680 --> 00:01:56,740
or is it just going to read the variable and replace the value?

36
00:01:56,740 --> 00:01:57,580
And it's the second.

37
00:01:57,580 --> 00:02:00,630
It's not going to update variable1 because we're not

38
00:02:00,630 --> 00:02:02,450
loading it back into variable1.

39
00:02:02,450 --> 00:02:05,550
So that will always say the word instructor,

40
00:02:05,550 --> 00:02:09,320
even though we did a replace at some point in the code.

41
00:02:09,320 --> 00:02:13,300
Now the second option would be to load that value into a new variable,

42
00:02:13,300 --> 00:02:14,840
which is actually quite common too.

43
00:02:14,840 --> 00:02:18,650
So we take an existing value in a variable that we don't want to change,

44
00:02:18,650 --> 00:02:21,450
but we want to use it somewhere else with changes.

45
00:02:21,450 --> 00:02:23,730
So instead of modifying the original,

46
00:02:23,730 --> 00:02:28,050
we simply say replacevariable = variable1 and then

47
00:02:28,050 --> 00:02:30,380
that replace syntax that we have.

48
00:02:30,380 --> 00:02:34,150
Now we also can use regex to swap things around.

49
00:02:34,150 --> 00:02:37,380
So in this instance, clearly Jones,

50
00:02:37,380 --> 00:02:40,050
Tom isn't the correct way around for the name.

51
00:02:40,050 --> 00:02:42,920
And so I want it to be Tom Jones.

52
00:02:42,920 --> 00:02:47,220
So what we can do here is you'll notice I'm making an adjustment,

53
00:02:47,220 --> 00:02:53,240
but I'm also updating variable2 because it says variable2 =.

54
00:02:53,240 --> 00:02:57,440
So I'm changing the original value of variable2.

55
00:02:57,440 --> 00:03:01,620
So variable 2 ‑replace, you can see in the quotes is the regex,

56
00:03:01,620 --> 00:03:04,460
which basically will say go and find the spaces,

57
00:03:04,460 --> 00:03:07,340
get the values, and then when you do this,

58
00:03:07,340 --> 00:03:09,550
it creates them into segments.

59
00:03:09,550 --> 00:03:12,870
So I have $2 and $1.

60
00:03:12,870 --> 00:03:16,440
$1 would be the first bit, which is Jones,

61
00:03:16,440 --> 00:03:21,240
and $2 is Tom because it's segmented into two components.

62
00:03:21,240 --> 00:03:27,640
Now when it outputs the variable2 value, it will be Tom Jones.

63
00:03:27,640 --> 00:03:32,340
We can also use replace using regex to remove any spaces.

64
00:03:32,340 --> 00:03:33,590
This one's quite a common one.

65
00:03:33,590 --> 00:03:36,600
I've seen multiple functions that people have written in PowerShell

66
00:03:36,600 --> 00:03:41,960
scripts simply using regex replacement to remove any spaces that

67
00:03:41,960 --> 00:03:44,460
may or may not be in a value because, of course,

68
00:03:44,460 --> 00:03:49,340
you can't often trust end‑user input.

69
00:03:49,340 --> 00:03:53,740
Now the next thing we can do here is to use that split operator.

70
00:03:53,740 --> 00:03:59,640
Now the split operator will split one or more strings into substrings.

71
00:03:59,640 --> 00:04:02,950
The default split delimiter is whitespace.

72
00:04:02,950 --> 00:04:05,630
So if you want to use commas or something else,

73
00:04:05,630 --> 00:04:08,600
you have to specify what you would use.

74
00:04:08,600 --> 00:04:14,140
Other delimiters are characters, strings, patterns, or script blocks.

75
00:04:14,140 --> 00:04:18,540
All substrings are returned by default.

76
00:04:18,540 --> 00:04:19,920
So what does this look like?

77
00:04:19,920 --> 00:04:24,780
Well, in the split operator syntax, it requires a couple of things.

78
00:04:24,780 --> 00:04:26,810
The first is the delimiter.

79
00:04:26,810 --> 00:04:28,230
The default obviously, as I mentioned,

80
00:04:28,230 --> 00:04:31,400
is the whitespace, but you can specify character strings,

81
00:04:31,400 --> 00:04:36,140
patterns, or even script blocks that then specify the delimiter.

82
00:04:36,140 --> 00:04:41,630
The split operator in PowerShell uses a regular expression in

83
00:04:41,630 --> 00:04:45,040
the delimiter rather than simple characters.

84
00:04:45,040 --> 00:04:49,900
You then need to specify the number of substrings, so this is the maximum

85
00:04:49,900 --> 00:04:53,970
number. So the default is to return every single substring.

86
00:04:53,970 --> 00:04:57,640
If you specify a number less than the number of substrings, the

87
00:04:57,640 --> 00:05:01,540
remaining ones are concatenated to the last one.

88
00:05:01,540 --> 00:05:04,950
And then, of course, we have the conditions the delimiter matches.

89
00:05:04,950 --> 00:05:09,150
So these are options that specify the conditions to

90
00:05:09,150 --> 00:05:10,700
which the delimiter is matched.

91
00:05:10,700 --> 00:05:15,040
So is it a multiline match or is it a simple match?

92
00:05:15,040 --> 00:05:17,640
So what does that look like in PowerShell?

93
00:05:17,640 --> 00:05:17,900
Well,

94
00:05:17,900 --> 00:05:23,140
in reality, it looks like this, ‑Split and then the string value,

95
00:05:23,140 --> 00:05:27,510
or ‑Split and then in brackets, in parentheses,

96
00:05:27,510 --> 00:05:32,570
the string value. It could also be String Split and

97
00:05:32,570 --> 00:05:37,240
then other values, so Delimiter, Max‑strings, and Options.

98
00:05:37,240 --> 00:05:42,310
It could also be String Split ScriptBlock to define the delimiter

99
00:05:42,310 --> 00:05:45,440
and then also the maximum number of substrings.

100
00:05:45,440 --> 00:05:48,440
Now that's great, but what does that look like in reality?

101
00:05:48,440 --> 00:05:49,460
Well, first off,

102
00:05:49,460 --> 00:05:53,010
if you want to split the string value using the default delimiter,

103
00:05:53,010 --> 00:05:53,830
we can do this.

104
00:05:53,830 --> 00:05:55,510
So, for example, I've got January, February,

105
00:05:55,510 --> 00:06:00,060
March, April, May June in a single text value with spaces in between.

106
00:06:00,060 --> 00:06:06,270
If I use the split option, it will break those out into discrete values, January,

107
00:06:06,270 --> 00:06:09,530
February, March, April, May, and June.

108
00:06:09,530 --> 00:06:13,620
If I was using comma separation, then I would have to change the syntax

109
00:06:13,620 --> 00:06:19,450
around. So you can see I'm now putting the text or the object first, split,

110
00:06:19,450 --> 00:06:22,870
and then after it is the delimiter that's being used.

111
00:06:22,870 --> 00:06:24,940
Same thing would happen, January, February,

112
00:06:24,940 --> 00:06:25,420
March, April,

113
00:06:25,420 --> 00:06:29,360
May, June, but it would split not on spaces, but on the

114
00:06:29,360 --> 00:06:32,240
delimiter that we specified of a comma.

115
00:06:32,240 --> 00:06:36,680
Now we can split the string values into three using comma delimiters.

116
00:06:36,680 --> 00:06:41,250
So now we're enhancing this. We're saying I want to take this January,

117
00:06:41,250 --> 00:06:42,090
February, March, April,

118
00:06:42,090 --> 00:06:45,330
May, June, split them by the comma, and then I also want

119
00:06:45,330 --> 00:06:51,340
to return three sets of those values.

120
00:06:51,340 --> 00:06:53,270
Now we could go a little bit differently here.

121
00:06:53,270 --> 00:06:56,290
We can actually just split a variable. So we can have

122
00:06:56,290 --> 00:06:58,330
an existing variable with values.

123
00:06:58,330 --> 00:07:00,760
So there's my variable, January, February,

124
00:07:00,760 --> 00:07:01,370
March, April, May,

125
00:07:01,370 --> 00:07:05,320
June, and then I simply say variable split to the

126
00:07:05,320 --> 00:07:08,540
delimiter that we wish to utilize.

127
00:07:08,540 --> 00:07:13,340
Now there's also what's referred to as the .Split function.

128
00:07:13,340 --> 00:07:18,740
This splits input into multiple substrings based on a delimiter.

129
00:07:18,740 --> 00:07:23,240
So instead of using ‑Split, which effectively is an operator,

130
00:07:23,240 --> 00:07:26,980
some objects will have .Split as a function.

131
00:07:26,980 --> 00:07:31,310
This uses whitespace characters, like space, tabs, and

132
00:07:31,310 --> 00:07:34,240
line‑breaks as the default delimiter.

133
00:07:34,240 --> 00:07:39,190
It's available for all [String] type variables and values.

134
00:07:39,190 --> 00:07:41,470
So this doesn't exist on an integer.

135
00:07:41,470 --> 00:07:43,940
It's only available to strings.

136
00:07:43,940 --> 00:07:46,140
So what does that look like in PowerShell?

137
00:07:46,140 --> 00:07:49,130
Well, if we have a variable, January, February,

138
00:07:49,130 --> 00:07:51,250
March, April, May, June, comma separated,

139
00:07:51,250 --> 00:07:56,850
you'll see that I can use variable.Split, so I'm calling the function,

140
00:07:56,850 --> 00:08:01,040
and then in parentheses, I specify the delimiter.

141
00:08:01,040 --> 00:08:04,450
I can also do nested split if I wanted to as well.

142
00:08:04,450 --> 00:08:07,840
So you'll see here that in my text data that's coming in,

143
00:08:07,840 --> 00:08:11,700
I'm using a mix of commas and semicolons too, which would

144
00:08:11,700 --> 00:08:16,830
mess up my split. So I can say variable.Split to the commas,

145
00:08:16,830 --> 00:08:22,340
.Split to the semicolons as well.

146
00:08:22,340 --> 00:08:25,650
Now let's talk about padding string values.

147
00:08:25,650 --> 00:08:27,220
It sounds like a strange thing,

148
00:08:27,220 --> 00:08:33,300
but we can pad values left, and we can pad values right. So

149
00:08:33,300 --> 00:08:36,750
the idea being is that the pad left will add padding of

150
00:08:36,750 --> 00:08:39,600
string values to the left of a value,

151
00:08:39,600 --> 00:08:44,500
and you can set the specified length you wish to add. For pad right,

152
00:08:44,500 --> 00:08:47,050
you can add padding to the right of the value,

153
00:08:47,050 --> 00:08:54,590
and you can also set the specified length. Now when padding string values,

154
00:08:54,590 --> 00:08:59,050
if we kind of have a look at the effect here. So I have a variable or a

155
00:08:59,050 --> 00:09:03,500
value, and then I have obviously the width, so the value length plus

156
00:09:03,500 --> 00:09:06,140
padding and then a padding character.

157
00:09:06,140 --> 00:09:09,340
So if my word was Demonstration,

158
00:09:09,340 --> 00:09:19,740
you can see that my value length would be 13, and then I want to pad to 1,

159
00:09:19,740 --> 00:09:25,340
and then I'm going to say padding character is A. So what this would do is

160
00:09:25,340 --> 00:09:30,560
this would add that A to one end or the other,

161
00:09:30,560 --> 00:09:33,440
depending on whether we chose left or right.

162
00:09:33,440 --> 00:09:36,240
If I do the same thing here, so sample.

163
00:09:36,240 --> 00:09:41,730
So it's taken the length, so 1, 2, 3, 4, 5, 6 + 1 = 7, so the

164
00:09:41,730 --> 00:09:45,840
seventh character would be padded with the letter B.

165
00:09:45,840 --> 00:09:53,270
If I do this with numbers, then you can see I've got 1 + 5 = 6.

166
00:09:53,270 --> 00:09:57,190
Now what would that look like because I have a single value, but then I'm

167
00:09:57,190 --> 00:10:02,000
padding with a character five times? So that would increase the width of that

168
00:10:02,000 --> 00:10:07,150
to be six characters. So it would be five 0s + 1.

169
00:10:07,150 --> 00:10:10,090
Now, of course, if we're doing variables, we can do the same thing as well.

170
00:10:10,090 --> 00:10:12,440
So I have a variable value.

171
00:10:12,440 --> 00:10:15,710
I'm going to get the length of the variable, add 5 to it,

172
00:10:15,710 --> 00:10:21,140
which equals X, whatever it would be, and I'm going to use T as the padding.

173
00:10:21,140 --> 00:10:25,740
So it's going to add the letter T five times to something.

174
00:10:25,740 --> 00:10:29,440
So padding is all about adding something. So a standard approach here

175
00:10:29,440 --> 00:10:34,650
would be I have a unique ID for a user, it's 23, but all of our employee

176
00:10:34,650 --> 00:10:39,840
records have six 0s before it. So you could simply pad the value, and it

177
00:10:39,840 --> 00:10:42,340
would give you what you're looking for.

178
00:10:42,340 --> 00:10:43,860
So how does that work here?

179
00:10:43,860 --> 00:10:48,290
So the standard syntax is using effectively functions like the

180
00:10:48,290 --> 00:10:53,280
split where it's .PadLeft, .PadRight, the Width,

181
00:10:53,280 --> 00:10:56,140
and then the character you wish to utilize.

182
00:10:56,140 --> 00:10:59,040
So if I was to use that Demonstration word,

183
00:10:59,040 --> 00:11:02,920
it would be variable = Demonstration PadLeft.

184
00:11:02,920 --> 00:11:05,830
I know it's 14, and so add an A.

185
00:11:05,830 --> 00:11:11,940
So that would put the word letter A at the beginning of the word Demonstration.

186
00:11:11,940 --> 00:11:13,610
If I was to pad right,

187
00:11:13,610 --> 00:11:19,000
it would do the same process, but put the letter B at the end of the word Demonstration.

