Now, let's look at ways of manipulating and working with strings.
Let's define a string, PowerShell, save as a $s.
Let's pipe $s to Get‑Member.
There are lots of methods here that you can see,
and we're going to go through some of them,
and one Property, Length, which shows me how long the string is.
So if I look at that, I can see that PowerShell is 10 characters long.
We looked at this in the lesson on arrays,
but remember,
strings can be treated as an array, so the first index of the string 0 is P,
or if I want to, I can use the range operator and say,
hey, get me elements 3 through 6, and there we go,
and that's treated then as another array.
You can take strings and make them say all lowercase,
or you can make them all uppercase.
When you invoke these methods,
this does not change the original or underlying object,
so $s hasn't changed.
All I did was invoke a method, and PowerShell wrote a new result to the pipeline.
Now, let's parse $s,
and there's a substring method that can be used a number of ways.
Let's take $s and get substrings starting at index 5.
Because I did not specify a length,
PowerShell just went and gave me the rest of the string starting at index 5.
Although if I wanted to, I could start at,
in this case, 0 and give me 5 characters.
It's really not that difficult to try to parse strings out.
Remember though, PowerShell is all about objects,
and I don't want you to be focusing on, oh,
how do I, I need to parse strings and parsing output.
This is the kind of thing that you might do if you have to parse a
log file, for example, or you need to tweak some output in some
custom objects that you are creating.
Don't try to look at a Powershell command,
and look at the output, and try thinking,
okay, how do I parse this as string output?
Don't go down that route.
So going back to this example,
$s.Substring(5) is just writing another object to the pipeline,
so I can say, hey, make that all uppercase.
Let me show you something else with strings,
and this is something that you will encounter.
So I'm going to create $t, and I'm going to create the string Pluralsight,
but I'm putting spaces around it.
Because when you're working with data in PowerShell you
may not always know where it comes from,
and you may need to clean it up, and that's what we're going to do here.
You can see the space before, and you really can't tell
that there is whitespace after the string.
I can look at the length property and see that it is 15.
That includes the whitespace.
PowerShell has a number of methods that you can use on strings.
You may, for example, only want to trim the end,
meaning, get rid of all the whitespace that is at the end of the string,
and it's kind of hard to see in this example, TrimStart
a little bit easier to see there, or you can use the Trim method,
which will trim the entire object, both start and end.
We'll save that to $r.
Now that looks right.
And if I look at the length property, I can see that it is 11,
so that previous length, obviously, were all whitespace characters.
And here's how you might use all of this.
So I'm going to create a variable to a txt file,
so my work folder,
and you have this file as part of the course downloads
so if you wanted to play with here.
So I do Get‑Content and see what's in that file.
So this is supposed to be a list of computer names.
I just have a localhost several times,
Offline for a computer that's not available,
and again, localhost.
And you can see,
I've got some blank lines and some extra spaces around localhost,
so I need to kind of clean up this file if I want to use it.
If I were to try to do this, for example,
Get‑Content $f and run Test‑Connection, Test‑Connection will then take
the contents of $f and use it for the computername.
The first one works because localhost has no spacing,
then I get some blank lines, I'll get an error for Offline,
and if you look at the messages there, you'll see Testing connection to,
and then in quotes, it's got localhost,
and you can actually see that there are spaces around that.
Well, actually, I don't want that.
So I want to be able to clean this up, and here's how we can do that.
So we'll do Get‑Content.
Now,
for each string that comes through from
Get‑Content, now this is one way to do it,
and I'm going to use, you know,
some new PowerShell 7 syntax we looked at earlier in the course.
If the length is greater than 0, then I'm going to run Test‑Connection,
and for the computername, use the current object,
but Trim all of the whitespace, both front and back,
on that; otherwise, that's the single colon there,
otherwise, I'm going to Write‑Warning "Skipping blank".
So there we go.
I can see that I get my results for localhosts,
I get skipping the blank lines,
and I get the error message for the computer that is Offline,
which is perfectly fine.
From a more practical perspective, this might be a better approach though.
Let's do Get‑Content again, get $f, where the length is greater than 0.
And then for each result,
I want to take the $_ against the current object in the pipeline,
so that string that comes through that has a length greater than 0,
trim it and use that as a computername for Test‑Connection.
Now, I don't worry about the blanks because those really don't matter to me.
I get the computers that work, localhosts,
and I get my error message for Offline.
That's probably an easier, cleaner way to do this.
Now,
the reason that I did not do Get‑Content pipe to Where object pipe to
Test‑Connection is because I needed to invoke that Trim method on
every object that came through the pipeline.
So I needed to use for each object in order to achieve that.
All right, next,
I want to show you about other things that we can do with strings.
So remember, we've got $s here, so it's PowerShell.
Strings also have a replace method.
Different ways that we can use it.
So I can say replace all the es with the number 3.
Again, this doesn't change $s.
It just writes a new string to the pipeline.
There is a replace method that we can use.
And one thing to notice about this method, it is case sensitive.
So if I try to replace the lowercase p with X, it doesn't work.
So I would have to do something like this,
ignore the casing of the string, and just replace any P,
capital case or lowercase with X.
Remember,
you will often be working with data and values from commands that you
have no way of knowing what is in that variable,
and so you may need to take certain things into account,
and this is one example.
So let me do this.
All right, see if you can follow this.
Remember we talked about the importance of leveraging the object notation.
So I'm going to take $s, and I'm going to replace o with the set of parentheses.
Remember, that's writing a new object of the pipeline.
I'm then going to replace that object,
replace the e with the greater than sign and then replace h with the &.
In other words, I'm going to make three replacements all at once,
and there we go.
Now,
another option though for replacing if we don't want to worry too much
about case sensitivity is use the replace operator.
So now, I can take $s and say replace P with the!, and that will work.
So the replace method is case sensitive unless you use that ordinal ignore case,
or you can use to replace method, which is not case sensitive.
The other reason that you might want to use to replace method
is that you can use regular expressions.
So I'm going to replace, in the string jeff789,
I'm going to replace any number with the letter X,
and there we go.
So with that in mind,
let's look at some ways that we can take advantage
of these operators and methods, and we create a string,
$data, and this might be data that you get from a log file.
Now, I can split that string using the split method into an array.
And because that is an array of object, and we saw this in the array module,
I can invoke a method,
and PowerShell will invoke this method on all members of the array.
So I can take the data, split it on the pipe,
or the vertical bar, that's going to leave some spaces,
but no problem because I'm going to trim it.
So if I look at my array, you can see, well,
it may be kind of hard to see, but you see,
I got rid of, for example, the leading space in front of deco,
so that's very useful.
See if you can follow what I'm doing here though.
Remember, $arr is an array, element 0 is the first element in the array,
art, art is a string, strings can be arrays,
so element 0 in art is letter a, right,
so let's see if you can follow along here.
I'm going to initialize an empty array, $name,
and I'm going to add to $name this command.
Let me type it out here, and let me explain then what we're doing.
All right, So I'm going to take the first element in the array $arr,
which is art, right, and I'm going to use the replace method.
Now,
I'm going to replace the first element in the array
and the first letter in the array, right,
a, the lowercase a,
and I'm going to replace that with basically the same character $arr[0][0].
Now, that is going to actually technically be a character object,
and so in order to use the ToUpper method,
I need to turn it to a string.
So I'm taking the first character in art,
turning it back into a string, and then invoking the ToUpper method.
This seems more complicated than it is,
but that's only because I'm trying to explain to you what is happening.
Eventually, this all becomes second nature, and you won't even think about it.
And then I'll do the same thing with the last name.
So the second element in the array $arr is deco.
And, again, I'm going to replace the first letter,
which is lowercase, with the first letter toString and make it uppercase.
So now, $name will have the first name and last name, all in proper case.
There are other ways that you could do this,
but I wanted to use this example to demonstrate these methods and how
you can use PowerShell to string things together. Once I have this, and
maybe this is part of a script or something that I'm building to
provision a user account, I could then display a message to the user,
writing Creating user, and I'm going to take that name array and join
it with spaces.
So now, I can say, hey,
I'm creating user art deco and feed that back to the
user so they know what is going on.
There are no cmdlets to do any of this.
There are methods and operators,
and I'm just using the PowerShell language to give me information that I need in order to do my job.