PowerShell is a versatile and powerful scripting language that can be used to automate various tasks on your system. One common task that often requires the use of PowerShell is working with strings. In this article, we’ll cover some of the basics of working with strings in PowerShell, including concatenation, formatting, and manipulation.

Concatenation – Putting Strings Together (especially with Variables)

Concatenation is the process of joining two or more strings together. In PowerShell, this can be done using the + operator. For example, let’s say we have two strings, “Hello” and “World”:

$greeting = "Hello"
$name = "World"
$fullGreeting = $greeting + " " + $name

The $fullGreeting variable will now contain the concatenated string “Hello World”. See how nice using the variables makes it when you’re reading code like this? Here’s more info on what’s up with variables in PowerShell.

Formatting – Editing and adjusting strings from within your script

PowerShell provides several ways to format strings, including using string interpolation and the Format-String cmdlet.

String Interpolation – Including variables inside a larger string

String interpolation allows you to embed variable values directly within a string. To use string interpolation, simply enclose the variable name in curly braces within a string that is enclosed in double quotes. For example:

$name = "Michael"
$age = "7,000"
Write-Host "My name is $name and I am $age years old."

The output of this code will be “My name is Michael and I am 7,000 years old.” There’s no problem with the comma in the number “7,000” because even though it’s a number, in this case it’s treated as a string, so it’s just a line of characters.

Format-String Cmdlet

The Format-String cmdlet provides more advanced string formatting capabilities. It allows you to specify the format of the output string using placeholders that are replaced with the values of the variables you provide. Here’s an example:

$name = "Michael"
$age = 7000
Write-Host ("My name is {0} and I am {1} years old." -f $name, $age)

The output of this code will be the same as the previous example: “My name is Michael and I am 7000 years old.” Almost. There is actually a really small difference between the two, and it actually makes a pretty big impact. The age is missing a comma in the output. But look at the input. The input is also missing the “double-quotes” which tells PowerShell that this is a string, even if it’s made up of numbers. So in this case, the “$age” variable is an integer.

PowerShell does some awesome work for us behind the scenes to make sure that we can still use it. PowerShell is kind of always looking out for us non-programmers and doing things like “Oh, you included an integer in the middle of a string? How bout I just turn it into a string for you so you can use it here.”

I swear, it’s like what I was saying when I wrote about how PowerShell is the BEST programming language and most important admin tool for us!

Manipulation – Hacking, slashing, and straight up mangling strings

PowerShell provides several ways to manipulate strings, including trimming, replacing, and splitting.

Trimming

To remove whitespace from the beginning or end of a string, you can use the Trim method. For example:

$name = "   Michael   "
$trimmedName = $name.Trim()

The $trimmedName variable will now contain the string “John”, with the leading and trailing spaces removed.

Replacing

To replace one or more characters within a string, you can use the Replace method. For example:

$name = "Michael"
$replacedName = $name.Replace("o", "a")

The $replacedName variable will now contain the string “Michoel”. A terrible thing to do. You could also replace bigger sections of the string, like “$replacedName = $name.replace(“Michael”,”YourName”)

Splitting

To split a string into an array of substrings based on a delimiter, you can use the Split method. For example:

$name = "Michael,Simmons"
$splitName = $name.Split(",")

The $splitName variable will now contain an array with two elements: “Michael” and “Simmons”. This is really helpful when you’re working with a single line from a CSV. But don’t get too deep in the weeds trying to break up lots of strings from a CSV, because there are some killer tools to help with those already built in.

Conclusion

In this article, we covered some of the basics of working with strings in PowerShell, including concatenation, formatting, and manipulation. By understanding these concepts, you’ll be able to more effectively automate tasks and manipulate data within your PowerShell scripts.

References

There are tons of options for getting this information. Blogs like mine are great at being conversational and giving you just the right amount of info on what you need to know… But if you need to go right to the official documentation, well… here’s ya go!