PowerShell: How to Rename File Extension of Multiple Files


You can use the following basic syntax to rename the file extension of multiple files in a directory using PowerShell:

Dir *.csv | Rename-Item -NewName { $_.Name -replace '\.csv$','.txt' }

This particular example renames all file extensions with an extension of .csv in the current directory with an extension of .txt instead.

The following example shows how to use this syntax in practice.

Example: How to Rename File Extension of Multiple Files in PowerShell

Suppose that our current directory has the following path:

  • C:\Users\bobbi\OneDrive\Documents\test_data

We can type Dir to list out all files in this current directory:

We can see that there are three files in this directory that all have .csv as the extension:

  • my_data.csv
  • my_new_data.csv
  • some_old_data.csv

Suppose that we would like to change the .csv extension to .txt for all three files.

We can use the following syntax to do so:

Dir *.csv | Rename-Item -NewName { $_.Name -replace '\.csv$','.txt' }

The following screenshot shows how to use this syntax in practice:

PowerShell rename file extension for multiple files

We can see that we were able to successfully change the .csv extension to .txt for all three files in the directory:

  • my_data.txt
  • my_new_data.txt
  • some_old_data.txt

Note that we used the command Dir *.csv to specify that we only want to change the file extension for files that had .csv as the extension.

If we had other files with different file extensions in the same directory, these files would be left untouched.

Related Tutorials

The following tutorials explain how to perform other common tasks in PowerShell:

PowerShell: How to Replace Every Occurrence of String in File
PowerShell: How to Replace Text with Wildcard
PowerShell: How to Replace Special Characters in String

Leave a Reply