PowerShell: How to Add Extension to Multiple Files

You can use the following basic syntax to find all files with a specific extension in a directory using PowerShell:

Get-ChildItem -File | Rename-Item -newname {$_.name + ".txt"}

This particular example will add .txt as a file extension to each file in the current directory.

Here is what the various commands do:

  • Get-ChildItem -File: Retrieves all files from current directory
  • Rename-Item -newname: Specifies that we’d like to rename each file
  • $_.name + “.txt”: Retrieves the filename of the current file and concatenates “.txt” to the end of it

Note that a plus sign ( + ) is used to concatenate strings together in PowerShell.

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

Example: How to Add Extension to Multiple Files in PowerShell

Suppose that our current directory has the following path:

  • c:\users\bobbi\data2

We can use Get-ChildItem -File to list all files in this current directory:

We can see that there are four files in the current directory and each of them currently have no extension.

Suppose that we we would like to add .txt as an extension to each of these files.

We can use the following syntax to do so:

Get-ChildItem -File | Rename-Item -newname {$_.name + ".txt"}

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

PowerShell add extension to multiple files

Notice that when we run the Get-ChildItem cmdlet again to view the updated items in the directory we can see that each file now has .txt as the file extension.

Feel free to replace “.txt” with any file extension that you would like.

Note: You can find the complete documentation for the Rename-Item cmdlet in PowerShell here.

Related Tutorials

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

PowerShell: How to Delete All Files with Specific Extension
PowerShell: How to Rename File Extension of Multiple Files
PowerShell: How to Get File Extension

Leave a Reply