The name of a file without the extension is needed.


The method GetFileNameWithoutExtension() from the .NET class System.IO.Path can be used for this task.

$fileNameWithoutExtension = [System.IO.Path]::GetFileNameWithoutExtension($filename)


If the file name is a full or partial path to the file, only the file name will be returned:

$filename = "C:\Temp\myfile.pdf"
$fileNameWithoutExtension = [System.IO.Path]::GetFileNameWithoutExtension($filename)

The result in $fileNameWithoutExtension would be "myfile".

The method GetFileNameWithoutExtension() only removes the last extension:

$filename = "file.dwg.pdf"
$fileNameWithoutExtension = [System.IO.Path]::GetFileNameWithoutExtension($filename)

The example above would return "file.dwg" in $fileNameWithoutExtension

To remove all extensions from a file name a loop can be used:

$filename = "C:\Temp\file.dwg.png.pdf"
while ($filename -ne [System.IO.Path]::GetFileNameWithoutExtension($filename)){
    $filename =  [System.IO.Path]::GetFileNameWithoutExtension($filename)
  • Define the name or the path of the file
  • Remove the first extension and then compare it to the original filename. If the strings are not equal remove the extension. If the strings are equal, the filename has no extension
  • Repeat the second step until there are no more extensions in the filename

In this case the result in $fileNameWithoutExtension would be "file".