How to Extract a Substring From a Filename in PowerShell

Have you ever had a bunch of files and needed just a part of the file names? Maybe you want to grab a date, a code, or just the middle part sandwiched between two dashes. If you’re using PowerShell, you’re in luck! Extracting substrings from filenames in PowerShell is not only possible — it’s easy and even fun!

Let’s walk through it together, step-by-step. We’ll keep the tech stuff simple. If you can slice a cake, you can slice a string. 🍰

🔍 Why Extract a Substring?

Imagine you’ve got files like:

Invoice_2023-04-01_ClientA.pdf
Invoice_2023-04-02_ClientB.pdf
Invoice_2023-04-03_ClientC.pdf

And all you want is the date in the filename. That’s where substring extraction comes in handy.

⚙️ Using Substring Method

Strings in PowerShell are like long lines of characters. You can cut out part of that line using the Substring() method.

Here’s a simple example:

$filename = "Invoice_2023-04-01_ClientA.pdf"
$substring = $filename.Substring(8, 10)
Write-Output $substring

What’s happening here?

  • $filename: Your complete filename as a string.
  • .Substring(8, 10): Starts at character 8 and grabs the next 10 characters.
  • Result: “2023-04-01” 🎉

That’s it! You’ve just surgically removed the date from the file name.

🧠 Bonus Tip: Use Length and Indexing

Not every filename is neat and predictable. Sometimes, you’ll wanna be even cooler and tell PowerShell to find where to start cutting, dynamically.

Example:

$filename = "Report-ClientX-2023.txt"
$start = $filename.IndexOf("ClientX")
$substring = $filename.Substring($start, 7)
Write-Output $substring

Output: “ClientX”

Pro tip: You can also find the length dynamically if the content varies using the LastIndexOf or measuring the string length.

✂️ Splitting Instead of Slicing

Sometimes slicing by index can feel tricky. Another easy trick? Use Split!

$filename = "Invoice_2023-04-01_ClientA.pdf"
$parts = $filename -split "_"
Write-Output $parts[1]

You’ll get “2023-04-01”. Just like that. 💥

This works wonders when your filenames use consistent separators like underscores, dashes, or periods.

🛠️ Combine It With Other Commands

So, you have 50 files and you need the date part from each. Use Get-ChildItem and loop through all of them:

Get-ChildItem "C:\Invoices" | ForEach-Object {
    $name = $_.Name
    $date = ($name -split "_")[1]
    Write-Output "Found date: $date"
}

This will go through every file in your folder and extract the date part from their names. Super handy!

✨ Summary – You’re Now a Substring Pro!

Let’s recap the cool new string tricks you’ve learned:

  • Substring(start, length): Classic slice method if you know where to cut.
  • IndexOf: Find where a word or part starts.
  • Split: Chop the string apart using a symbol like “_”.

Using these tools, you’ll be able to sift, sort, and slice any filename like a PowerShell ninja. 🥷

Next time you see a long messy file name, don’t stress. Just grab your PowerShell console, take a deep breath, and think: “Which part do I want?” 🎯

Then, snip away! 🔪 Happy scripting!