135

I want to extract filename from below path:

D:\Server\User\CUST\MEA\Data\In\Files\CORRECTED\CUST_MEAFile.csv

Now I wrote this code to get filename. This working fine as long as the folder level didn't change. But in case the folder level has been changed, this code need to rewrite. I looking a way to make it more flexible such as the code can always extract filename regardless of the folder level.

($outputFile).split('\')[9].substring(0)

10 Answers 10

239

If you are ok with including the extension this should do what you want.

$outputPath = "D:\Server\User\CUST\MEA\Data\In\Files\CORRECTED\CUST_MEAFile.csv"
$outputFile = Split-Path $outputPath -leaf
1
  • 8
    This is a great answer. It solves the problem in the most "built-in" way. However, for my needs, I needed both the filename with extension and the base filename, so I used @angularsen's answer. There's another parameter, -leafbase, but it's only supported on PowerShell Core 6+.
    – Jamie
    Aug 23, 2018 at 20:01
92

Use .NET:

[System.IO.Path]::GetFileName("c:\foo.txt") returns foo.txt. [System.IO.Path]::GetFileNameWithoutExtension("c:\foo.txt") returns foo

3
39

Using the BaseName in Get-ChildItem displays the name of the file and using Name displays the file name with the extension.

$filepath = Get-ChildItem "E:\Test\Basic-English-Grammar-1.pdf"

$filepath.BaseName

Output:

Basic-English-Grammar-1


$filepath.Name

Output:

Basic-English-Grammar-1.pdf

0
6

Find a file using a wildcard and get the filename:

Resolve-Path "Package.1.0.191.*.zip" | Split-Path -leaf
5
$(Split-Path "D:\Server\User\CUST\MEA\Data\In\Files\CORRECTED\CUST_MEAFile.csv" -leaf)
0
5
Get-ChildItem "D:\Server\User\CUST\MEA\Data\In\Files\CORRECTED\CUST_MEAFile.csv"
|Select-Object -ExpandProperty Name
4

You can try this:

[System.IO.FileInfo]$path = "D:\Server\User\CUST\MEA\Data\In\Files\CORRECTED\CUST_MEAFile.csv"
# Returns name and extension
$path.Name
# Returns just name
$path.BaseName
1
  • $path.DirectoryName retuns D:\Server\User\CUST\MEA\Data\In\Files\CORRECTED
    – elpezganzo
    Mar 21, 2022 at 17:25
4

You could get the result you want like this.

$file = "D:\Server\User\CUST\MEA\Data\In\Files\CORRECTED\CUST_MEAFile.csv"
$a = $file.Split("\")
$index = $a.count - 1
$a.GetValue($index)

If you use Get-ChildItem to get the "fullname", you could also use "name" to just get the name of the file.

2
  • 9
    Simply use the index -1 if you're taking the array approach: $file.Split("\")[-1] Mar 5, 2016 at 12:16
  • If we want to remove the extension what can we do? Oct 16, 2020 at 8:04
4

Just to complete angularsen's answer that uses .NET.

In this code the path is stored in the %1 argument (which is written in the registry under quote that are escaped: \"%1\" ). To retrieve it, we need the $arg (inbuilt arg). Don't forget the quote around $FilePath.

# Get the file path:
$FilePath = $args
Write-Host "FilePath: " $FilePath

# Get the complete file name:
$file_name_complete = [System.IO.Path]::GetFileName("$FilePath")
Write-Host "fileNameFull :" $file_name_complete

# Get file name without the extension:
$fileNameOnly = [System.IO.Path]::GetFileNameWithoutExtension("$FilePath")
Write-Host "fileNameOnly :" $fileNameOnly

# Get the file extension:
$fileExtensionOnly = [System.IO.Path]::GetExtension("$FilePath")
Write-Host "fileExtensionOnly :" $fileExtensionOnly
2
$file = Get-Item -Path "c:/foo/foobar.txt"
$file.Name

Works with both relative and absolute paths

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.