14

I have an absolute path in a variable in my powershell 2.0 script. I want to strip off the extension but keep the full path and file name. Easiest way to do that?

So if I have C:\Temp\MyFolder\mytextfile.fake.ext.txt in a variable called, say $file

I want to return

C:\Temp\MyFolder\mytextfile.fake.ext

5 Answers 5

26

if is a [string] type:

 $file.Substring(0, $file.LastIndexOf('.'))

if is a [system.io.fileinfo] type:

join-path $File.DirectoryName  $file.BaseName

or you can cast it:

join-path ([system.io.fileinfo]$File).DirectoryName  ([system.io.fileinfo]$file).BaseName
3
  • It is a [string] type. If I wanted to make it more robust and cope with a variable extension length? What if I have a .pl file or a .blahblah file? Feb 8, 2013 at 10:39
  • $file.Substring(0, $file.LastIndexOf('.')) is a good one. But mind 2 cases: 1) no extension and a dot in the middle: 'C:\Tests.1\FileNoExtension' results in 'C:\Tests'; 2) no dots at all results in an exception. Feb 8, 2013 at 11:55
  • @RomanKuzmin You're right... thank you. I hope that who read my answer read your comment too and know all his files have an extesion or using code with cast to [Fileinfo] ;)
    – CB.
    Feb 8, 2013 at 12:00
22

Here is the best way I prefer AND other examples:

$FileNamePath
(Get-Item $FileNamePath).Extension
(Get-Item $FileNamePath).Basename
(Get-Item $FileNamePath).Name
(Get-Item $FileNamePath).DirectoryName
(Get-Item $FileNamePath).FullName
3
  • 13
    Because there are more efficient solutions instead of complex code.
    – Antebios
    Jan 5, 2017 at 13:42
  • 2
    It would be nice if you provided some comments beside each line showing what exactly would be returned.
    – deadlydog
    Nov 12, 2017 at 17:36
  • A big drawback here is that this requires the file to exist. Jan 6, 2019 at 8:46
6
# the path
$file = 'C:\Temp\MyFolder\mytextfile.fake.ext.txt'

# using regular expression
$file -replace '\.[^.\\/]+$'

# or using System.IO.Path (too verbose but useful to know)
Join-Path ([System.IO.Path]::GetDirectoryName($file)) ([System.IO.Path]::GetFileNameWithoutExtension($file))
6
  • 2
    If you have access to the file you could also use: Get-Item $yourpathvar | % { $_.FullName -replace $_.Extension }
    – Frode F.
    Feb 8, 2013 at 10:55
  • 1
    @Graimer: I think this is not correct. 1) string $_.Extension may also be in the middle of the path. 2) It may contain special regex characters like []. Feb 8, 2013 at 10:58
  • I'm sorry that I didn't see the hidden regex characters in his sample. MOST people don't use special characters in an extension. As for your point one, please show me a sample where the extension is in the middle of a path(which exists). I'm not saying mine was better, just a easier to read version for normal day use :)
    – Frode F.
    Feb 8, 2013 at 11:06
  • 2
    In 'C:\Tests.1\Test.1' after replacing '.1' we will get 'C:\Tests\Test' (not correct). And, yes, it exists, I have just created one :) Feb 8, 2013 at 11:11
  • 2
    @David Martin suggests yet another way using get-item BUT the file must exist: Get-Item $file | %{ join-path -path $_.DirectoryName -ChildPath $_.basename}. It is worth to be posted as a separate answer but @C.B.'s version is very similar and does not require a file to exist and be obtained. Feb 8, 2013 at 13:24
6

You should use the simple .NET framework method, instead of cobbling together path parts or doing replacements.

PS> [System.IO.Path]::GetFileNameWithoutExtension($file)

https://msdn.microsoft.com/en-us/library/system.io.path.getfilenamewithoutextension%28v=vs.110%29.aspx

1
  • 4
    OP asked how to get the file path without an extension. This does not do that. Rather, ::GetFileNameWithoutExtension() returns the file name stripped of both the path and the extension.
    – alx9r
    Dec 26, 2016 at 18:08
3

Regardless of whether $file is string or FileInfo object:

(Get-Item $file).BaseName
1
  • 1
    This requires that the file actually exists.
    – Josh
    Aug 7, 2019 at 20:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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