fbpx

PowerShell Substring Secrets Revealed: Become a Command-Line Wizard

The PowerShell substring () method in PowerShell can be used to extract a portion of a string. The start and length of the substring we wish to extract from a string can be specified using this function.


The PowerShell substring Substring method will be described in detail in this article, along with a few examples of how to extract certain strings using the startIndex and/or length functions.

Methods to Extract powershell substring from a String

  1. PowerShell Substring Method
  2. Finding a PowerShell substring Using the Length Property
  3. Working with Dynamic Strings
  4. Using IndexOf in Substring

1. PowerShell Substring Method

The Substring() method in PowerShell can be used to locate a string within another string. Every string object in PowerShell has this method.

The syntax of the PowerShell substring Method:

string.substring(int startIndex, int length)

As you can see from the Syntax, you must enter both startIndex AND length when specifying a string’s substring method. StartIndex and length are both numbers (integers), as indicated by the syntax’s use of the int symbol.


The first character of the substring you want to extract is the startIndex. counting from left to right and beginning at 1, except for the first character, adding 1 to the IndexOf result.

Using the well-known string “hello world” as our starting point will assist illustrate this strategy the best. Use the command below to solely extract the word “world” from the string:

"Hello World".Substring(6)

Only the substring’s starting point, 6, has been supplied. In this manner, the function will extract all characters in the string beginning at position 6.

"Hello World".Substring(6)

I have numbered the location beneath each letter of the string to help clarify the placement of each character. As you can see, the letter W is at position 6.

Hello World
012345678910

When only the word Hello is to be extracted from the string, we must supply a startIndex of 0 and a length of 5 characters:

"Hello World".Substring(0,5)

2. Finding a Substring Using the Length Property

You might need to locate the PowerShell substring made up of the final four characters. The group of characters from the fourth to the last place all the way to the conclusion must be located. Any length can be used for the string that you are searching.

We can dynamically set the end position by using the length of the string and subtracting a specific number of characters from it, as opposed to declaring the end position as a positive number counting from the left.

You might want to locate the final four characters using the string $code = ‘KJBKBSKJ252125’. Rather than doing something similar:

$code = 'KJBKBSKJ252125'
$code.SubString(0,4)
$code.SubString(0,4)
$code.SubString(0,4)

0 can be changed to $code.Length – 4 will return the last four characters even if the end position isn’t used at all.

$code = 'KJBKBSKJ252125'
$code.SubString($product_code.Length-4)
$code = 'KJBKBSKJ252125'
$code.SubString($product_code.Length-4)
$code = 'KJBKBSKJ252125'
$code.SubString($product_code.Length-4)


The PowerShell substring technique will always default to the last character position if you don’t specify the end position. You can dynamically choose substrings by subtracting from the length property of the string, which represents the total number of characters in the string.

3.  Working with Dynamic Strings

You may not always be aware of the string’s length when working with dynamic strings. We can accomplish this by utilizing the substring method’s string length function.

Let’s say we want to extract the filenames without the extension from a folder containing log files. The filename can be any length, however in this case the extension is 3 letters and a dot.

In order to reduce the filename length to just the filenames, we can do the following:

$filename = "la-ams-file02-log-1.log"
$filename.Substring(0, $filename.Length - 4)

This can likewise be used in reverse to extract only the filename extensions. We supply a length of 3 characters for the filename, which we use to calculate the start position:

$filename = "la-ams-file02-log-1.log"
$filename.Substring($filename.Length - 3, 3)

In this instance, we may simply return the remaining letters and don’t even need to specify the length of the substring to return (3):

$filename.Substring($filename.Length - 3)

4. Using IndexOf in Substring

There are occasions when you wish to extract a section in the middle of a string when working with dynamical string lengths. Finding or calculating the precise starting place for the substring approach is then the difficult part.

Utilizing IndexOf is one method for doing this. Using this technique, you may determine where a character or string is located within a string.


We’ll look in the string for the part that is distinct before the date-log. In this situation, we will need to add 4 characters to it because the IndexOf method will return the starting point of the string or character that you looked for:

$files = @(
    'la-ams-ad01-log-202223.log',
    'la-ams-ad01-log-2022113.log',
    'la-osl-ad01-log-2022113.log',
    'la-osl-file01-log-202214.log'
)
$files | Foreach {
    # Get the position of the substring 'log-' in each filename and add 4 to this position.
    $part = $_.Substring($_.IndexOf('log-') + 4)
    # Returns 202223.log for example

    # Remove the extension from the substring
    $date = $part.Substring(0, $part.Length - 4)
    write-host $date
    # Returns 202223 for example
}

In this specific situation, you may also use the method LastIndexOf() in the file names that contain hyphens:

$files | Foreach {
    $part = $_.Substring($_.LastIndexOf('-') + 1)
    write-host $part
    $date = $part.Substring(0, $part.Length - 4)
    write-host $date
}