PowerShell:
Extracting substrings

How to:

Here’s how to slice and dice strings in PowerShell:

# Given a string
$text = "Power up your PowerShell skills!"

# Extracting using substring method
$startIndex = 10
$length = 9
$substring = $text.Substring($startIndex, $length)
Write-Host $substring  # Output: your Powe

# Extracting using range operator
$subrange = $text[10..18] -join ''
Write-Host $subrange  # Output: your Powe

# Extracting from the start until some position
$firstPart = $text.Substring(0, $startIndex)
Write-Host $firstPart  # Output: Power up 

# Extracting after a certain character
$splitString = $text.Split(" ")[2]
Write-Host $splitString  # Output: your

Deep Dive

Way back, PowerShell rocked just basic string methods. Now, it’s a different ball game. The .Substring() method has been around and is pretty straightforward—give it a start index and an optional length, and it’ll snip out what you need. Starting in PowerShell 6, you can also use the range operator, which can be simpler, especially when you’re handling variable-length strings.

There’s also the -split operator and .Split() method, both handy for cutting up strings based on patterns or characters. Need a specific chunk? Use these tools.

Performance-wise, there’s not much in it for small tasks. When you’re working with huge text files or looping every millisecond, you’ll want benchmarks. Otherwise, it’s more about readability and what feels right for your script.

Remember, PowerShell strings are indexed starting at zero—common in many programming languages. Watch out for the pesky off-by-one error.

See Also

For more on string manipulation in PowerShell: