How to rename multiple files using PowerShell

On Windows 10, PowerShell is a powerful scripting tool, which similar to Command Prompt, allows you to rename files virtually any way you want. These instructions, we'll walk you through the most common scenarios to rename one as well as various files at once.

Renaming single file

To rename a single file using PowerShell, use these steps:

  1. Open Start.

  2. Search for PowerShell and click the top result to open the app.

  3. Navigate to the folder with the files to rename.

    This example opens the "Documents" folder:

    cd Documents

  4. Type the following command to change the name of a single file and press Enter:

    Rename-Item "OLD-FILE-NAME.EXTENSION" "NEW-FILE-NAME.EXTENSION"

    In the command, the quotation marks are only required if the name includes spaces.

    This example renames the file to "beach_trip_notes.txt":

    Rename-Item "surfing_trip_notes.txt" "beach_trip_notes.txt"

    image.png

Renaming multiple files in bulk

To rename multiple files in bulk when the name structure isn't important, use these steps:

  1. Open Start.

  2. Search for PowerShell and click the top result to open the app.

  3. Navigate to the folder with the files to rename.

  4. Type the following command to rename multiple files in bulk and press Enter:

    Dir | %{Rename-Item $_ -NewName ("NEW-FILE-NAME{0}.EXTENSION" -f $nr++)}

    This example renames images with a ".jpg" extension using the same ("beach_trip_2019") naming scheme and appends a different number at the end of the name:

    Dir | %{Rename-Item $_ -NewName ("beach_trip_2019{0}.jpg" -f $nr++)}

image.png

Trimming multiple file names

To make file names shorter, or trim part of the names by an N number of characters, use these steps:

  1. Open Start.

  2. Search for PowerShell and click the top result to open the app.

  3. Navigate to the folder with the files to rename.

  4. Type the following command to rename files using shorter names and press Enter:

    Dir | Rename-Item -NewName {$_.name.substring(0,$_.BaseName.length-N) + $_.Extension}

    In the command update "$_.BaseName.length-N" by changing the value of "N" to specify the number of characters that you want to remove.

    This example will trim the name of your files by eight characters:

    Dir | Rename-Item -NewName {$_.name.substring(0,$_.BaseName.length-8) + $_.Extension}

    image.png

Deleting part of the name from multiple files

To remove part of the file name on multiple files with PowerShell, use these steps:

  1. Open Start.

  2. Search for PowerShell and click the top result to open the app.

  3. Navigate to the folder with the files to rename.

  4. Type the following command to remove part of the file name and press Enter:

    Dir | Rename-Item -NewName {$_.name -replace "OLD-FILE-NAME-PART",""}

    This example removes the word "trip" from the name of all files in the folder:

    Dir | Rename-Item -NewName {$_.name -replace "trip",""}

    image.png

Replacing part of the name from multiple files

To rename the same part of the file name on similar files, use these steps:

  1. Open Start.

  2. Search for PowerShell and click the top result to open the app.

  3. Navigate to the folder with the files to rename.

  4. Type the following command to replace part of file name and press Enter:

    Dir | Rename-Item -NewName {$_.name -replace "OLD-FILE-NAME-PART,"NEW-FILE-NAME-PART"}

    This example replaces the word "beach_" for "surfing_trip" of all the files that contain the word "tower" as part of the file name:

    Dir | Rename-Item -NewName {$_.name -replace "beach_","surfing_trip"}

    image.png

Removing spaces from multiple files

Spaces on file names can sometimes cause problems, especially when using a command terminal. If you have files using spaces in the title, you can modify the name to include a visual separator, such as dash or underscore symbol.

To remove and replace spaces with underscores, use these steps:

  1. Open Start.

  2. Search for PowerShell and click the top result to open the app.

  3. Navigate to the folder with the files to rename.

  4. Type the following command to remove spaces from file name and press Enter:

    Dir | Rename-Item -NewName { $_.Name -replace "SPACE","SEPARATOR" }

    This example replaces spaces with underscores in all files:

    Dir | Rename-Item -NewName { $_.Name -replace " ","_" }

    image.png

Changing file extension

To change the file extension for a bunch of files with PowerShell, use these steps:

  1. Open Start.

  2. Search for PowerShell and click the top result to open the app.

  3. Navigate to the folder with the files to rename.

  4. Type the following command to change the extension on files and press Enter:

    Dir | Rename-Item -NewName { [io.path]::ChangeExtension($_.name, "NEW-EXTENSION") }

    This example changes any file extension to ".doc":

    Dir | Rename-Item -NewName { [io.path]::ChangeExtension($_.name, "doc") }

    image.png

Renaming specific extension file names

The instructions outlined above will rename every file within the location. However, if you want to rename a particular file format, such as documents, images, or videos, then you can use the "-filter" option.

To change the names of a specific file format, use these steps:

  1. Open Start.

  2. Search for PowerShell and click the top result to open the app.

  3. Navigate to the folder with the files to rename.

  4. Type the following command to rename files with a specific extension and press Enter:

    Dir -filter *.EXTENSION | %{Rename-Item $_ -NewName ("NEW-FILE-NAME{0}.EXTENSION" -f $nr++)}

    This example renames only files that include the ".jpg" extension:

    Dir -filter *.jpg | %{Rename-Item $_ -NewName ("beach_trip_{0}.jpg" -f $nr++)}

    image.png

Once you complete the steps, PowerShell will rename the files of a specific extension using the name that you specified in the command.

Although these commands have been tested to work correctly, it's always recommended that you perform a test run before trying to rename the original files.

Original: https://www.windowscentral.com/how-rename-multiple-files-bulk-windows-10#rename-files-using-powershell

你可能感兴趣的:(How to rename multiple files using PowerShell)