0
0
PowershellHow-ToBeginner · 2 min read

PowerShell Script to Extract Substring Easily

Use $string.Substring(startIndex, length) in PowerShell to extract a substring from a string, where startIndex is the starting position and length is how many characters to take.
📋

Examples

Input"HelloWorld", startIndex=0, length=5
OutputHello
Input"PowerShell", startIndex=5, length=4
OutputShell
Input"Test", startIndex=2, length=10
Outputst
🧠

How to Think About It

To extract a substring, think of the string as a row of letters. You pick where to start counting (startIndex) and how many letters to take (length). PowerShell's Substring method does exactly this by slicing the string from the start position for the given length.
📐

Algorithm

1
Get the input string.
2
Decide the starting position (startIndex) for the substring.
3
Decide how many characters (length) to extract.
4
Use the substring method to get the part of the string.
5
Return or print the extracted substring.
💻

Code

powershell
$string = "PowerShell"
$startIndex = 5
$length = 4
$substring = $string.Substring($startIndex, $length)
Write-Output $substring
Output
Shell
🔍

Dry Run

Let's trace extracting substring 'Shell' from 'PowerShell' starting at index 5 with length 4.

1

Set variables

$string = 'PowerShell', $startIndex = 5, $length = 4

2

Extract substring

$string.Substring(5, 4) returns 'Shell'

3

Output result

Print 'Shell'

OperationValue
Input stringPowerShell
Start index5
Length4
Extracted substringShell
💡

Why This Works

Step 1: Using Substring method

The Substring method extracts part of a string starting at startIndex for length characters.

Step 2: Start index is zero-based

Counting starts at zero, so index 5 means the 6th character in the string.

Step 3: Handles length exceeding string end

If length is longer than remaining characters, it returns up to the string's end without error.

🔄

Alternative Approaches

Using string slicing with range operator
powershell
$string = "PowerShell"
$substring = $string[5..8] -join ''
Write-Output $substring
This uses array slicing and joining characters, which is less direct but useful for character ranges.
Using -replace with regex
powershell
$string = "PowerShell"
$substring = ($string -replace '^.{5}(.{4}).*$', '$1')
Write-Output $substring
This uses regex to capture substring but is more complex and less readable.

Complexity: O(k) time, O(k) space

Time Complexity

Extracting a substring takes time proportional to the length of the substring k, as it copies those characters.

Space Complexity

The substring requires extra space proportional to its length k, as it creates a new string.

Which Approach is Fastest?

Using Substring is the fastest and most readable. Alternatives like slicing or regex are slower and more complex.

ApproachTimeSpaceBest For
Substring methodO(k)O(k)Simple and direct substring extraction
Array slicingO(k)O(k)When working with characters as arrays
Regex replaceO(n)O(k)Complex patterns or conditional extraction
💡
Remember that string indexes start at zero in PowerShell when extracting substrings.
⚠️
Beginners often forget that startIndex is zero-based, causing off-by-one errors.