The PowerShell Substring: Finding a string inside a string

Adam Bertram

Read more posts by this author.

A common scenario in an admin’s world is to figure out a way to find a certain snippet of text inside a string; called the substring. PowerShell makes finding a substring extremely easy.

Related: Learn the String Format and Expanding Strings

PowerShell and Strings

In the PowerShell world, a string consists of a set of characters enclosed with single or double-quotes. Strings like "foo" and 'bar' are extremely common.

Related: Back to Basics: PowerShell Strings

Let’s say you’ve defined a string in a variable and only need to find a part of it? For example, let’s say you’ve got a string with an address like 1234 4th St. You’d like to pull out the number and know that the first four characters will always be the number you need. In this instance, you can use the PowerShell substring() method.

Using the PowerShell SubString Method

To find a string inside of a string with PowerShell, you can use the Substring() method. This method is found on every string object in PowerShell.

For example, perhaps you have a string like The quick brown fox jumped over the fence. You’d like to find the first five characters. You could do that using the Substring() method like so:

$string = 'The quick brown fox jumped over the fence.'

The first argument to pass to the Substring() method is the position of the leftmost character. In this case, the leftmost character is T. The second argument is the farthest rightmost character position. In this case, the character is q.

The Substring() method returns all of the characters in between them.

Here’s a real-world example:

Let’s say we’ve got a product code that has the following format: XXXXVVVV-MM-DD-YYYY. All products have this code format and never deviate from it. Now let’s say the inventory management software that generated this code didn’t include a date created field in the database.

However, the software always put this date as MM-DD-YYYY in the product code itself so we can infer this from the product code. We need to get this date for our products to create some super-fancy yet extremely unnecessary report to management to show when each product was inserted into the database. Here’s a good way to get this done.

$product_code = 'ABCD1234-11-12-2013'
$date_created = [DateTime]$product_code.SubString($product_code.Length-10)

In this example, we’re using the PowerShell SubString() method for the inherent string object and passing the number for the first character we want to find as the parameter.

Since we’re not specifying the stopping character it assumes we want all characters to the end of the string. To find the number in the first place, we’re first getting the length of the entire product code and simply subtracting 10 from that. In this example, this means we want to start at the 10th character from the right of the string and everything to the right of that.

The [DateTime] type doesn’t necessarily go with the substring topic of the post but I thought it’d fit in nicely with the specific example.

After we get our string of 11-12-2013 we then type cast it to a [DateTime] object which converts what was once a dumb string into a nice object. From here, we can do all the fancy date arithmetic management wants us to do on it and generate that fancy bar chart report!

Dynamically Finding a Substring Using the Length Property

In the above example, you were statically defining the start and end positions of the characters inside of the string. But what if you don’t know the last position?

Perhaps you need to find the substring from the last four characters. You need to find the set of characters from the fourth to the last position all the way to the end. The string you’re searching in could be any length.

Instead of defining the end position as a positive number which counts from the left, we can dynamically specify the end position using the length of the string and deducting a certain number of characters from it.

Using the string above $product_code = 'ABCD1234-11-12-2013', maybe you want to find the last four characters. Instead of doing something like this:

PS> $product_code = 'ABCD1234-11-12-2013'
PS> $product_code.SubString(0,4)

You can replace 0 with $product_code.Length - 4 and not even use the end position at all and it will return the last four characters as shown below.

PS> $product_code = 'ABCD1234-11-12-2013'
PS> $product_code.SubString($product_code.Length-4)

If you don’t specify the end position, the PowerShell substring method will always default to the last character position.

Using the length property of the string which is the total number of characters in the string and deducting from that count allows you to dynamically pick out substrings.

Subscribe to Stay in Touch

Never miss out on your favorite ATA posts and our latest announcements!

Looks like you're offline!