PowerShell, as a language, is known for its object-oriented approach. Yet a lot of commands within the language and its .NET support still allow the user to massage and manipulate text (strings) in some ways.
Profile gravatar of Adam Bertram

Adam Bertram

Adam is a Microsoft Cloud and Datacenter Management Most Valuable Profressional (MVP) who specializes in Windows PowerShell. You can reach Adam at adamtheautomator.com or on Twitter at @adbertram.
Profile gravatar of Adam Bertram
Contents of this article

When working with strings in PowerShell, we've got lots of options depending on what we're trying to accomplish. We've got ways to find text within the text, edit text, and add text. Each of these categories entails some different commands and techniques.

For this article, we'll touch on finding text and adding text. More specifically, let's go over how to use simple string matching and regular expressions (regex) as well as appending strings to existing object properties. To keep it simple, we'll hold everything in memory here, so we won't be going over how to write this text to files or pull text from a database, for example.

Finding strings ^

Within PowerShell, we've essentially got two ways to find strings: regex or not regex. The first method gives you the ability to find anything in anything but may leave you curled up in a corner rocking back and forth. We also have the latter, which isn't as powerful but will leave you with some hair after you're done.

Matching strings without regex

The easiest way to find a string embedded in another is by using the like operator. The like operator understands direct string matching like 'foo' -like 'foo' (which equals True by the way). It also supports popular matching characters like the asterisk as well.

For example, perhaps I have a string that looks like this:

I'd like to find out if this string contains the word is in it. One way to do that is by using the like operator like so:

Matching strings with regex

Another more powerful yet harder to understand method is to use regex. The .NET regex engine is built into a lot of different areas in PowerShell like the match operator or the Select-String command.

Using the example above, perhaps I'd like to get stricter in my match requirements and check to see if the strings st4ing, stUing, or stKing exist to ensure I don't have a typo. We need to match the original string except for the third character of string. For this character, we need to match a 4, a U, or a K. To perform more complicated matching like this, we need to use regex. We'll first use the match operator.

Match operator

Match operator

The match operator uses the regex engine to perform its string matching, unlike the like operator. However, if you're not familiar with regex, exactly how that match came to be is probably a mystery. There are entire books on regex, but plenty of sites out there teach the basics.

Similarly, we could also use the Select-String command like this and then look at the automatic $matches variable to see if it was successful.

Select String

Select String

Concatenating strings ^

Creating strings in PowerShell is easy. Just type '' and voilà, you've made a string! But how about concatenating strings? You can join strings together in PowerShell a few different ways. The easiest way and one you've most likely seen before is by inserting a variable inside of a double-quoted string.

We can also concatenate strings together by using the + operator.

If you find yourself needing to insert lots of variables in a string, you may find that using string formatting is a more sensible approach. String formatting allows you to insert tokens in the original string, which PowerShell replaces with elements when processing the string. For example, perhaps I have a string like this:

Perhaps I want to make this a template and be able to replace Adam quickly with any first name and PowerShell with some other topic. Using string formatting would be a "cleaner" approach.

String formatting consists of replacing each element you'd like to change with a numbered token and then using the -f parameter to specify an array of values you'd like to replace those token placeholders with. In the example above, I have two elements I need to replace. In that case, we will number them 0 and 1, which we can represent like this:

I can now add the -f parameter and include each of the elements I'd like to replace those tokens with.

You can manipulate text (strings) in a nearly infinite number of ways in PowerShell. It's important to realize that no matter what format the data you face, there's always a way to massage that text in a manner so that it ends up in a format you need!

Take part in our competition and win $100!

Share
3+

Users who have LIKED this post:

  • avatar
  • avatar

Related Posts

0 Comments

Leave a reply

Your email address will not be published. Required fields are marked *

*

CONTACT US

Please ask IT administration questions in the forum. Any other messages are welcome.

Sending
© 4sysops 2006 - 2017

Log in with your credentials

or    

Forgot your details?

Create Account