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.
Output:
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:
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:
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.
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:
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:
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:
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.Pathwhen you only need to parse a path string. - Use
Get-Itemwhen the file exists and you also want file metadata. - Prefer
.Name,.BaseName, and.Extensionover 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.

