PowerShell: How to Rename Multiple Folders


Often you may want to rename multiple folders based on a particular pattern in PowerShell.

You can use the Rename-Item cmdlet with the following syntax to do so:

Dir c:\users\bobbi\dat\ | Rename-Item -NewName { $_.Name -replace '_','-' }

This particular example renames all folders in the directory that we specified by replacing the underscore ( _ ) in each folder name with a dash ( ) instead.

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

Example: How to Rename Multiple Folders in PowerShell

Suppose that we would like to rename multiple folders in the following directory:

  • c:\users\bobbi\dat

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

We can see that there are four folders in this directory that each have an underscore in the folder name:

  • east_data
  • north_data
  • south_data
  • west_data

Suppose that we would like to rename all of these folders by replacing the underscore in each folder name with a dash instead.

We can use the following syntax to do so:

Dir c:\users\bobbi\dat\ | Rename-Item -NewName { $_.Name -replace '_','-' }

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

PowerShell rename multiple folders

We can see that we were able to successfully rename each folder in the directory:

  • east-data
  • north-data
  • south-data
  • west-data

Note that if there were any folders that did not have an underscore in the folder name then those folder names would simply be left unchanged.

Related Tutorials

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

PowerShell: How to Rename File Extension of Multiple Files
PowerShell: How to Copy and Rename a File
PowerShell: How to Find All Files with Specific Extension

Leave a Reply