≡ Menu

Get FileName with or without extension from full path using PowerShell

It is a very common requirement to have just file name extracted from full path while working on your PowerShell scripts. For example, your script received a file full path(c:\abc\xyz\test.txt) and you want to just get the file name from it with or without extension(ex: test or test.txt). In this post how to achieve this.

There are several ways to do this. For example, a programmer with good regex knowledge tend to write a regex to extract the file name. Or you can also use Split-Path cmdlet in PowerShell do this. The approach I am going to share here is based on .Net classes and my favourite approach. I am not against using cmdlets but thing is they also internally use the .Net classes. So why not use them directly?

Get File name from full path.


Get File name with extension from full path


As you can see I am using System.IO.Path classes to perform this work. You can explore the other methods available in these modules and there are many useful functions.


Comments on this entry are closed.