HomeBlogExplaining PowerShell Substring With Practical Script Examples

Explaining PowerShell Substring With Practical Script Examples

Author

Date

Category

PowerShell is powerful, but its commands can be confusing at first. One of the neat built-in features is the Substring() method. It’s super handy when you want to grab part of a string. Think of it like cutting words out of a sentence with digital scissors. Sounds fun, right? Let’s break it down with simple examples you can actually use.

What is Substring?

The Substring() method lets you extract part of a string. Want the first 5 letters of a word? Want to skip the first 3 letters and grab the next 4? No problem! Substring is your best friend here.

Here’s the basic format:

.Substring(startIndex, length)
  • startIndex: where to start (0-based!)
  • length: how many characters you want

If you skip the length, it grabs everything from the startIndex to the end.

Let’s Try a Simple Example!

$string = "PowerShellRocks"
$result = $string.Substring(0, 5)
Write-Output $result

Output:

Power

Ta-da! You just sliced out the first five characters. 🎉

a close up of a computer screen with code on it powershell code editor terminal substring

Skipping Characters

Now say we don’t want the first few characters. We want to cut in the middle. Here’s how:

$string = "PowerShellRocks"
$result = $string.Substring(5, 5)
Write-Output $result

Output:

Shell

See what happened? We started at index 5 and grabbed 5 letters. Boom. Done.

What About Just the End Part?

You can leave out the length too:

$string = "PowerShellRocks"
$result = $string.Substring(10)
Write-Output $result

Output:

Rocks

That’s right. It grabbed everything after index 10. Super easy!

Real-World Use Case: Parsing Email Domains

Let’s say you’ve got an email address and you want just the domain part. Something like this:

$email = "user@example.com"
$atIndex = $email.IndexOf("@")
$domain = $email.Substring($atIndex + 1)
Write-Output $domain

Output:

example.com

Clever, right? We find where “@” is, add 1, and grab the rest.

square hole formation email string powershell slice domain

Another Fun Example: Format Phone Numbers

Ever seen a messy phone number like “1234567890”? Let’s format it like (123) 456-7890 using substring magic:

$number = "1234567890"
$area = $number.Substring(0,3)
$prefix = $number.Substring(3,3)
$line = $number.Substring(6)
$formatted = "($area) $prefix-$line"
Write-Output $formatted

Output:

(123) 456-7890

Looks professional, right? You can impress your boss now. 😎

Oops! Common Mistake

What happens if you ask for more characters than the string has?

$string = "Short"
$result = $string.Substring(0, 10)

Error! PowerShell will complain 😬

To avoid this, use the Length property:

if ($string.Length -ge 10) {
  $result = $string.Substring(0, 10)
}
else {
  $result = $string
}
Write-Output $result

Now it’s safe. No crashing today!

Recap Time!

Here’s what we learned:

  • Substring() filters parts of a string
  • You define where to start and how long to go
  • You can format, slice, and clean up text with it

Substring is your Swiss Army knife for strings. Use it wisely, and soon you’ll be slicing through PowerShell problems like a pro.

Want more string tricks? Stay tuned! PowerShell has plenty more fun in store.

Recent posts