So now that we've looked at formatting and casting,
one of the things that we do is actually manipulate data,
and we're going to focus on string data, so we're talking text values.
So common string manipulation,
normally the most common one is replacing string values.
So you may start with, let's say, a sentence or a few characters,
and you wish to replace something in that value.
Next would be splitting string values.
This is very, very common. You know, as an example,
if you're managing something on a network and you
need to retrieve domain accounts,
often the biggest thing is splitting out the domain
and the dash and the user account,
or maybe it's an email format you want to split out.
Then, of course, you've got padding string values.
This one is less used,
but this allows you to pad values at either end of a string value.
So the first thing we have is the ‑replace operator.
So this takes an input string, then the replace operator,
then there's a matching pattern, and then there's the string replacement.
So that's kind of the four key pieces of the replace operator.
So let's look at how we use the replace operator.
So first off, we have two variables.
variable1, which is a sentence,
The class instructor asked for a volunteer for a demonstration,
and then variable2 is a name, so Jones, Tom,
Tom Jones, whatever it would be.
Now if we're using the replace operator,
we can take variable1, and we can replace the value inside of it.
Now think carefully about this one.
Variable1 is the containing object.
If I then say variable1 ‑replace instructor,
teacher, the thing you have to remember is,
is that going to update variable1,
or is it just going to read the variable and replace the value?
And it's the second.
It's not going to update variable1 because we're not
loading it back into variable1.
So that will always say the word instructor,
even though we did a replace at some point in the code.
Now the second option would be to load that value into a new variable,
which is actually quite common too.
So we take an existing value in a variable that we don't want to change,
but we want to use it somewhere else with changes.
So instead of modifying the original,
we simply say replacevariable = variable1 and then
that replace syntax that we have.
Now we also can use regex to swap things around.
So in this instance, clearly Jones,
Tom isn't the correct way around for the name.
And so I want it to be Tom Jones.
So what we can do here is you'll notice I'm making an adjustment,
but I'm also updating variable2 because it says variable2 =.
So I'm changing the original value of variable2.
So variable 2 ‑replace, you can see in the quotes is the regex,
which basically will say go and find the spaces,
get the values, and then when you do this,
it creates them into segments.
So I have $2 and $1.
$1 would be the first bit, which is Jones,
and $2 is Tom because it's segmented into two components.
Now when it outputs the variable2 value, it will be Tom Jones.
We can also use replace using regex to remove any spaces.
This one's quite a common one.
I've seen multiple functions that people have written in PowerShell
scripts simply using regex replacement to remove any spaces that
may or may not be in a value because, of course,
you can't often trust end‑user input.
Now the next thing we can do here is to use that split operator.
Now the split operator will split one or more strings into substrings.
The default split delimiter is whitespace.
So if you want to use commas or something else,
you have to specify what you would use.
Other delimiters are characters, strings, patterns, or script blocks.
All substrings are returned by default.
So what does this look like?
Well, in the split operator syntax, it requires a couple of things.
The first is the delimiter.
The default obviously, as I mentioned,
is the whitespace, but you can specify character strings,
patterns, or even script blocks that then specify the delimiter.
The split operator in PowerShell uses a regular expression in
the delimiter rather than simple characters.
You then need to specify the number of substrings, so this is the maximum
number. So the default is to return every single substring.
If you specify a number less than the number of substrings, the
remaining ones are concatenated to the last one.
And then, of course, we have the conditions the delimiter matches.
So these are options that specify the conditions to
which the delimiter is matched.
So is it a multiline match or is it a simple match?
So what does that look like in PowerShell?
Well,
in reality, it looks like this, ‑Split and then the string value,
or ‑Split and then in brackets, in parentheses,
the string value. It could also be String Split and
then other values, so Delimiter, Max‑strings, and Options.
It could also be String Split ScriptBlock to define the delimiter
and then also the maximum number of substrings.
Now that's great, but what does that look like in reality?
Well, first off,
if you want to split the string value using the default delimiter,
we can do this.
So, for example, I've got January, February,
March, April, May June in a single text value with spaces in between.
If I use the split option, it will break those out into discrete values, January,
February, March, April, May, and June.
If I was using comma separation, then I would have to change the syntax
around. So you can see I'm now putting the text or the object first, split,
and then after it is the delimiter that's being used.
Same thing would happen, January, February,
March, April,
May, June, but it would split not on spaces, but on the
delimiter that we specified of a comma.
Now we can split the string values into three using comma delimiters.
So now we're enhancing this. We're saying I want to take this January,
February, March, April,
May, June, split them by the comma, and then I also want
to return three sets of those values.
Now we could go a little bit differently here.
We can actually just split a variable. So we can have
an existing variable with values.
So there's my variable, January, February,
March, April, May,
June, and then I simply say variable split to the
delimiter that we wish to utilize.
Now there's also what's referred to as the .Split function.
This splits input into multiple substrings based on a delimiter.
So instead of using ‑Split, which effectively is an operator,
some objects will have .Split as a function.
This uses whitespace characters, like space, tabs, and
line‑breaks as the default delimiter.
It's available for all [String] type variables and values.
So this doesn't exist on an integer.
It's only available to strings.
So what does that look like in PowerShell?
Well, if we have a variable, January, February,
March, April, May, June, comma separated,
you'll see that I can use variable.Split, so I'm calling the function,
and then in parentheses, I specify the delimiter.
I can also do nested split if I wanted to as well.
So you'll see here that in my text data that's coming in,
I'm using a mix of commas and semicolons too, which would
mess up my split. So I can say variable.Split to the commas,
.Split to the semicolons as well.
Now let's talk about padding string values.
It sounds like a strange thing,
but we can pad values left, and we can pad values right. So
the idea being is that the pad left will add padding of
string values to the left of a value,
and you can set the specified length you wish to add. For pad right,
you can add padding to the right of the value,
and you can also set the specified length. Now when padding string values,
if we kind of have a look at the effect here. So I have a variable or a
value, and then I have obviously the width, so the value length plus
padding and then a padding character.
So if my word was Demonstration,
you can see that my value length would be 13, and then I want to pad to 1,
and then I'm going to say padding character is A. So what this would do is
this would add that A to one end or the other,
depending on whether we chose left or right.
If I do the same thing here, so sample.
So it's taken the length, so 1, 2, 3, 4, 5, 6 + 1 = 7, so the
seventh character would be padded with the letter B.
If I do this with numbers, then you can see I've got 1 + 5 = 6.
Now what would that look like because I have a single value, but then I'm
padding with a character five times? So that would increase the width of that
to be six characters. So it would be five 0s + 1.
Now, of course, if we're doing variables, we can do the same thing as well.
So I have a variable value.
I'm going to get the length of the variable, add 5 to it,
which equals X, whatever it would be, and I'm going to use T as the padding.
So it's going to add the letter T five times to something.
So padding is all about adding something. So a standard approach here
would be I have a unique ID for a user, it's 23, but all of our employee
records have six 0s before it. So you could simply pad the value, and it
would give you what you're looking for.
So how does that work here?
So the standard syntax is using effectively functions like the
split where it's .PadLeft, .PadRight, the Width,
and then the character you wish to utilize.
So if I was to use that Demonstration word,
it would be variable = Demonstration PadLeft.
I know it's 14, and so add an A.
So that would put the word letter A at the beginning of the word Demonstration.
If I was to pad right,
it would do the same process, but put the letter B at the end of the word Demonstration.