---
title: "Substring Searches in PowerShell: Quick Guide"
description: "Easily find substrings within strings using PowerShell. Get the guide to the substring method."
canonical: "https://adamtheautomator.com/powershell-substring/"
---

# Substring Searches in PowerShell: Quick Guide

> Easily find substrings within strings using PowerShell. Get the guide to the substring method.

Source: https://adamtheautomator.com/powershell-substring/

---

ATA Learning

Tap to hide

[

ATA Learning

](/)

*   [Home](/)
*   [Tutorials](/tutorials/)
*   [Instructors](/author/)
*   [Advertising](/advertising/)
*   [Recommended Resources](/resources/)
*   [About Adam](/about-adam/)

Search for:  

*   [](https://twitter.com/adbertram)
*   [](https://github.com/Adam-the-Automator)
*   [](https://www.linkedin.com/company/adam-the-automator-llc)
*   [](/feed/)

![Substring Searches in PowerShell: Quick Guide](https://adamtheautomator.com/wp-content/uploads/2019/06/5d238ae5c824b514689ea587.jpg)

# Substring Searches in PowerShell: Quick Guide

[![](https://secure.gravatar.com/avatar/d0b9d42e21e5622713f8b693aa5c0f9244d5f7dd200ed29b8398f52dee5de337?s=192&d=mm&r=g)Adam Bertram](https://adamtheautomator.com/author/adam-bertram/)29 June 20193 min. read

Categories: [IT Ops](/category/it-ops/)

Tags:[PowerShell](/tag/powershell/)

Table of Contents

*   [PowerShell and Strings](#powershell-and-strings)
*   [Using the PowerShell SubString Method](#using-the-powershell-substring-method)
*   [Dynamically Finding a Substring Using the Length Property](#dynamically-finding-a-substring-using-the-length-property)

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](https://adamtheautomator.com/powershell-string-format/)_**

> _Announcing a [Free LIVE training](https://academy.viamonstra.com/courses/mini-course-starting-your-powershell-journey?ref=a52838) – Starting your PowerShell Journey – presented by Johan Arwidmark. Understand how PowerShell skills enhance your IT career, learn where to start with PowerShell, build your first scripts, and ask Johan questions directly in a live training environment._

## PowerShell and Strings

In the [PowerShell](https://adamtheautomator.com/tag/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](https://adamtheautomator.com/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:

```powershell
$string = 'The quick brown fox jumped over the fence.'
$string.Substring(0,5)
```

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.

```powershell
$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:

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

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.

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

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.

> __Announcing a [Free LIVE training](https://academy.viamonstra.com/courses/mini-course-starting-your-powershell-journey?ref=a52838) – Starting your PowerShell Journey – presented by Johan Arwidmark. Understand how PowerShell skills enhance your IT career, learn where to start with PowerShell, build your first scripts, and ask Johan questions directly in a live training environment.__

Share this article

[Share on X](https://twitter.com/intent/tweet?url=https%3A%2F%2Fadamtheautomator.com%2Fpowershell-substring%2F&text=Substring%20Searches%20in%20PowerShell%3A%20Quick%20Guide)[Share on Facebook](https://www.facebook.com/sharer/sharer.php?u=https%3A%2F%2Fadamtheautomator.com%2Fpowershell-substring%2F)[Share on LinkedIn](https://www.linkedin.com/sharing/share-offsite/?url=https%3A%2F%2Fadamtheautomator.com%2Fpowershell-substring%2F)

## Related Posts

![](https://adamtheautomator.com/wp-content/uploads/2026/06/26995-troubleshoot-dns-issues-powershell-codex.webp)

### [Troubleshoot DNS Issues with PowerShell](/troubleshoot-dns-issues-powershell/)

Troubleshoot DNS issues with PowerShell by testing name resolution, DNS client settings, cache entries, and network connectivity in a repeatable workflow.

![](https://adamtheautomator.com/wp-content/uploads/2025/10/image_2025-10-24_095322075.png)

### [Migrating from PowerShell 6 to 7.5: Breaking Changes/New Features](/migrating-powershell-6-to-7-5/)

Migrate from PowerShell Core 6 to 7.5: breaking changes, features, and testing tips.

![](https://adamtheautomator.com/wp-content/uploads/2025/06/featured-image-6.png)

### [How to Add Timeouts to Pester Tests with PowerShell Runspaces](/pester-test-timeout-runspaces/)

Prevent Pester tests from hanging indefinitely using PowerShell runspaces. Learn to handle variable scoping, module loading, TestDrive access, and stream capture challenges with timeout protection.

## Categories

*   [IT Ops](/category/it-ops/)
*   [Cloud](/category/cloud/)
*   [DevOps](/category/devops/)
*   [Home Ops](/category/home-ops/)
*   [Information Security](/category/infosec/)
*   [Software Development](/category/software-development/)

## Site

*   [Home](/)
*   [Tutorials](/tutorials/)
*   [Instructors](/author/)
*   [Advertising](/advertising/)
*   [Recommended Resources](/resources/)
*   [About Adam](/about-adam/)

Copyright 2026© ATA Learning | [Privacy Policy](/privacy/)
