powershell
file manipulation
filename extraction
file extension
scripting

powershell - extract file name and extension

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

In PowerShell, extracting a file name or extension is usually a path-manipulation problem, not a text-parsing problem. The safest tools are either the .NET System.IO.Path methods or PowerShell objects such as Get-Item, depending on whether the path must exist on disk. Choosing the right tool keeps the script shorter and avoids fragile string splitting.

Use System.IO.Path for Pure Path Parsing

If you already have a path string and do not need to touch the filesystem, System.IO.Path is usually the best option.

powershell
1$path = 'C:\Logs\archive\report.csv'
2
3$fileName = [System.IO.Path]::GetFileName($path)
4$fileNameWithoutExtension = [System.IO.Path]::GetFileNameWithoutExtension($path)
5$extension = [System.IO.Path]::GetExtension($path)
6
7$fileName
8$fileNameWithoutExtension
9$extension

Output:

text
report.csv
report
.csv

This works even if the file does not actually exist, because the code is only parsing the string.

Use Get-Item When the File Exists

If the path points to a real file and you want file-system metadata too, Get-Item is convenient:

powershell
1$item = Get-Item 'C:\Logs\archive\report.csv'
2
3$item.Name
4$item.BaseName
5$item.Extension

This is very readable, but it depends on the path existing. If the file is missing, Get-Item throws an error.

That difference is important in deployment or cleanup scripts. If you are working with candidate paths that may or may not exist yet, string parsing with System.IO.Path is usually safer than touching the filesystem early.

Handle Multiple Files in a Pipeline

PowerShell becomes especially useful when you need to process many files:

powershell
1Get-ChildItem 'C:\Logs' -File | ForEach-Object {
2    [PSCustomObject]@{
3        FullPath   = $_.FullName
4        FileName   = $_.Name
5        BaseName   = $_.BaseName
6        Extension  = $_.Extension
7    }
8}

This produces structured output that is easy to sort, filter, or export.

Understand What Counts as the Extension

GetExtension and .Extension return the last extension segment. For example, archive.tar.gz returns .gz, not .tar.gz.

powershell
$path = 'C:\Backups\archive.tar.gz'
[System.IO.Path]::GetExtension($path)

If your script needs compound extensions such as .tar.gz, you must handle that rule yourself.

Avoid Manual String Splitting

It is tempting to write something like:

powershell
$path.Split('\')[-1].Split('.')[0]

That usually works until it does not. Paths can contain multiple dots, different separators, or edge cases such as files without extensions. Built-in path helpers already handle those cases more consistently than ad hoc string splitting.

Files Without Extensions

If a file has no extension, the path helpers return an empty string for the extension:

powershell
$path = 'C:\Temp\LICENSE'
[System.IO.Path]::GetExtension($path)

That behavior is easy to test and safer than relying on a split operation that assumes a dot is always present.

The same helpers also work well with relative paths:

powershell
$path = '.\reports\summary.txt'
[System.IO.Path]::GetFileNameWithoutExtension($path)

That is useful in scripts where the current working directory changes and you still want path parsing logic that stays predictable.

Common Pitfalls

One common mistake is using Get-Item for a path that may not exist. If all you need is parsing, System.IO.Path is more robust because it does not depend on the filesystem.

Another mistake is manually splitting on dots to find the extension. That breaks for names with multiple dots or no extension at all.

Developers also sometimes forget that .Extension returns only the last suffix. If your workflow treats .tar.gz as a single logical extension, you need custom logic for that case.

Finally, avoid mixing path parsing with string-manipulation assumptions about separators. Built-in path helpers are clearer and less error-prone.

Summary

  • Use System.IO.Path when you only need to parse a path string.
  • Use Get-Item when the file exists and you also want file metadata.
  • Prefer .Name, .BaseName, and .Extension over manual string splitting.
  • Be aware that the extension helper returns only the last suffix segment.
  • Built-in path APIs are safer than ad hoc string parsing for file names and extensions.

Course illustration
Course illustration

All Rights Reserved.