File management is important to anyone working seriously with PCs. With this being the case, Windows provides several ways to manage your files. You can add files to folders, rename the folders, rename the files etc.

In this tutorial, I will show you an example of how you can rename or remove prefixes from Multiple files using PowerShell. To make it easy to understand what I am trying to do, I have video files with the same prefixes in their name followed by the number in the sequence. For example, Video-01.mp4, Video-02.mp4, Video-03.mp4 and so on. I want to remain with the sequence numbers as the name of the video files. Let's see how we can remove the prefix "Video-" from the files and remain with only the sequence numbers (01.mp4, 02.mp4)

Let's get started.

Streamlining File Management

As you know PowerShell is a very powerful tool for automating various tasks. This is what we need to rename the files without doing it manually. It would take forever to delete the prefixes if you have over a hundred video files that should follow the same naming convention.

You can write a command that does the heavy lifting for you. Let's write the command.

Before proceeding, ensure you have a backup of your files incase something goes wrong and also ensure you have the necessary permissions to rename the files in the target directory.

Utilizing the Power of Regular Expressions.

  • Start by opening PowerShell. You can do this by pressing Win + X and Select "Terminal"

  • Next, navigate to the folder that has your files using the cd command.

    cd C:\Path\To\Your\Files
    
  • Once you are in the right directory, run the command below to remove the common prefix from multiple files.

    Get-ChildItem *.extension | Rename-Item -NewName { $_.Name -replace '^Prefix_' }
    

    Note:

    Adjust the file extension (*.mp4) accordingly. In my example, I have mp4 video files. If they were text files, I would replace it with (*.txt).

    Also, change the prefix ('Prefix_') with the prefix you want to remove.

    Example:

    Get-ChildItem *.mp4 | Rename-Item -NewName { $_.Name -replace '^Video-' }
    

Explanation of the Command

When you run the command above, PowerShell will get all the files in the current directory that start with "Video_" and have the ".mp4" extension and then replaces the Video- with an empty string.

  • The first part of the command Get-ChildItem *.mp4 = This part retrieves a list of files with the specified extension. In my case it is mp4 files.
  • The second part | = this is a pipe operator. Its purpose is to take the output of the command on the left side and send it as the input to the command on its right.
  • The third part Rename_Item = This is a cmdlet that is used to rename an item.
  • The fourth part -NewName { $_.Name -replace '^Video-' } = This command specifies the new name for each file.
  • Inside the curly brace we have the $_ which represent the current item in the list of files. This means that $_.Name access the name property of the current file.
  • -replace = Now that we have the name of the current file in the list, we use the -replace regular expressing pattern to match the prefix we choose and replace it with an empty string. Hence removing that prefix from the name of the file. In our case, we started with files that have Video-01.mp4, we have specified the prefix to be ^Video- thus we end up with 01.mp4.

Replacing the Prefix with something Else

If you wish to replace the prefix with another prefix, add the word you want to add the name at the end of the command as shown below.

Get-ChildItem *.mp4 | Rename-Item -NewName { $_.Name -replace '^Video-', 'NewPrefix' }

The first prefix will be replaced with the second prefix in this case 'NewPrefix'.

Renaming file extensions

We make a slight change to the command to rename file extensions. Let's say we want to rename a .mv4 file to .mp4 , here is the command we use.

Get-ChildItem *.m4v | Rename-Item -NewName { $_.Name -replace '\.m4v$', '.mp4' }

So what has changed?

We've changed the first part of the command Get-Children * to include .mv4 extension. So this command will retrieve all files with the .mv4 extension.

We also modify the -replace part to take into account the file extensions.

In this modified command, the regular expression \.mp4$ is used to match the .mp4 extension at the end of the file name, and it is replaced with .avi. Adjust the extension accordingly based on your requirements.

  • \.m4v$ is a regular expression that matches the .m4v extension at the end of the file name.

This command only works for files inside the current directory. To include files in subdirectories, we need to do it recursively.

Renaming file Extensions Recursively

Here is how we modify the command above to recursively change file extensions

Get-ChildItem *.m4v -Recurse | Rename-Item -NewName { $_.Name -replace '\.m4v$', '.mp4' }

We add the -Recursive parameter with the Get-Children cmdlet.

This command will now search for files not only in the current directory but also in its subdirectories.

Conclusion

When managing a huge list of files and they all use a similar naming format, it is easier to use the power of PowerShell to simplify your work. In this article, you have learnt how to remove prefixes in bulk from files. If you found the article helpful, consider sharing the article.

Thank you for reading : )

Table of Contents
Great! Next, complete checkout for full access to GeekBits.
Welcome back! You've successfully signed in.
You've successfully subscribed to GeekBits.
Success! Your account is fully activated, you now have access to all content.
Success! Your billing info has been updated.
Your billing was not updated.