Many times when working with PowerShell, I find myself needing to manipulate text, or more properly, "string objects". There are a ton of ways you can manipulate strings, and a lot of those are thanks to the built-in methods included on all string objects. I'm not going to dive into all of them (I'll just cover some of my favorite go-tos), but you can find all of them with Get-Member. Here is what that looks like:
The methods I'm going to cover are: ToLower/ToUpper, Split (and concatenation), Replace, PadLeft/PadRight, and Trim/TrimStart/TrimEnd.
This is probably the simplest string manipulation method: it literally does what it says and turns text into upper or lower case.
There really isn't a lot more to say about this, other than it's a simple way to convert case.
While it is possible to convert text to TitleCase, it isn't a string method and I won't cover it on this post.
Split takes the string and breaks it apart into several strings. By default, it splits the string at whitespace characters. You can do this simple split by giving it no arguments (leave the parentheses empty).
You can also split a string at specific characters. Let's say the text you're working with is separated by commas or semicolons (this is a pretty common scenario), and you need to break it apart by that character. You do this by passing Split that character as a parameter (inside the parentheses).
Combine this with indexing (selecting a particular item in an array), and you can get really granular, cutting out a specific chunk of text out of a larger string. This example is a real-world use of this, and is exactly how I used to do that before learning Regular Expressions. In it, I first split by the comma character (,), select the first item (), split again by the equal sign (=), and select the second item ().
Split is probably the most powerful string manipulation trick you'll learn, at least until you learn Regular Expressions.
Concatenation isn't a string method, but I'm including it here because it is a natural companion to Split. It's simple: use the addition operator (+) between two strings to put them together.
Just remember that concatenation does not automatically add spaces, so if you expect there to be a space between the strings, you need to manually add them.
Simple concatenation works, but it can be hard to read, and easy to forget spaces (as demonstrated above). My preference is a method called String Interpolation, where you can embed a string directly inside another string. For this, you have to use double quotes (") instead of single quotes ('). You do it by literally just adding the variable into the string.
This is basically the string's "Find and Replace" method, and it works pretty much the same as any other "Find and Replace": you give it a string that you want to replace, and the string you want to replace it with. It will replace every instance of the first string that it finds with the second string.
Pad and Trim
These are the methods I use the least, but they are still invaluable on the occasions you need them. When you need them, there typically isn't any other particularly easy way to do what they do, so you'll be glad you have them available to you.
PadLeft and PadRight are used for adding "padding" to either the left or right of a string. This is useful when you have a minimum length something can be. For example, if you have employee numbers and the system you're putting them into expects them to be at least 8 characters long, but you started counting them at 1; in this example, you could use padding to ensure they are the minimum length necessary.
There are two ways to use these methods: with a single argument representing the minimum length of the string (it will add whitespace characters to the specified side), or with two arguments representing the minimum length (first argument, as above) and the character to use for padding.
TrimStart and TrimEnd remove characters from the beginning or end of a string, respectively. Without any arguments, it will trim whitespace characters on the specified side. You can also pass it an argument specifying which character or characters you want to trim. Whatever you specify here will be removed from that side of the string.
Trim does the same to both the start and end of the string.
Further Reading and Conclusion
As mentioned above, in place of using things like Split and Replace, I now tend to prefer using Regular Expressions (not always; Split and Replace are much quicker and easier for many small tasks). Those are a pretty big topic, though, so I'm not going to cover them in this post.
String manipulation is a surprisingly common task in PowerShell, so it's well worth getting good at it. The good news is, it's also insanely easy to practice, since all you need is a string and a PowerShell console. I recommend you practice using these methods to get a feel for how they work.
Let me know if there's anything you feel I missed and I'll see about making an update down the line.