Remove First Few Characters from Multiple Files at Once [Windows]

I had 78 markdown files (.md) in 2021-04-25-rename-multiple-files.md format and I wanted to remove the date from all the files at once. And, the final format should be like rename-multiple-files.md format.

Renaming all the files one-by-one manually would take ages, and after researching a bit I found the perfect solution that I needed.

I was able to remove the first 11 characters (to remove the date from the filenames) from all the 78 .md files in the folder.

Let me explain everything step-by-step:

Remove the First Few Characters from Multiple Files at Once in Windows 10

How to remove the first few characters from multiple files at once in Windows

I might not be able to describe properly, but it’s actually very easy.

Step 1 – Open the destination folder

First, open the folder where those files are located.

Press and hold the button shift and then right-click anywhere in the folder and select Open PowerShell window here option.

A new window will pop open and then proceed to the next step.

I’m on Windows 10 but for some older Windows, you might see CMD or Command Prompt instead of PowerShell.

Step 2 – Run the Command

Before running the command, it’s important to know what type of files you would be renaming – is it a .txt file? .mp4, .md, .docx, or any other.

Also, you need to count the number of characters you would remove from the left side. Please note that a dash or space in the filename would also be counted as 1 character.

Now that you know:

  • file extension (.mp4, or .txt, .docx, or any other)
  • the number of characters to trim from each file

It’s time to run the following command (but do not forget to edit before running):

get-childitem *.txt | rename-item -newname { [string]($_.name).substring(5) }

Assumption: If you have multiple text files (.txt) and want to remove the first 5 characters from the filename, then only the above command will work.

Example

Let’s assume you have multiple image files (.jpeg) in the folder and you want to remove the first 2 characters from the start then the command would be:

get-childitem *.jpeg | rename-item -newname { [string]($_.name).substring(2) }

And, that’s it.

Within seconds, all your files will be renamed.

Related: Remove the Last Few Characters from Multiple Files at Once in Windows

Please note that this process is irreversible, so I would recommend that you copy all the files to another folder and then try running these commands to avoid any mistakes.

Also, please note that this trick will work for files of the same extension. If you have to rename multiple files, you can repeat the process for each type of files.

If you get stuck somewhere, please let me know in the comments below.

Comments

40 responses to “Remove First Few Characters from Multiple Files at Once [Windows]”

  1. KAMAL Avatar
    KAMAL

    Thanks and appreciation Deepak!

    1. Deepak K Avatar

      Glad it helped, Kamal.

  2. Emily Avatar
    Emily

    Super easy. Clear instructions that worked. Renamed a couple hundred files in seconds. Thanks!

    1. Deepak K Avatar

      Thanks, Emily. Glad that it worked for you.

  3. Luke Hasenjaeger Avatar
    Luke Hasenjaeger

    This worked amazing, thank you so much!

    Is there a similar way to remove “x” number of characters from the end of the file name? Or search and replace in Windows Explorer in a similar way that Microsoft Excel does?

    1. Deepak K Avatar

      Hi Luke, glad it worked for you.

      I haven’t yet figured out how to remove chars from the end but I’ll update as soon as I do.

      Thank you.

      1. Shamaan Avatar
        Shamaan

        Get-ChildItem *.mp4 | rename-item -NewName {$_.name.substring(0,$_.BaseName.length-63) + $_.Extension -replace “_”,” “}

        This code works by replacing the characters counting from the end backwards
        i dont take credit though i found this on a subreddit
        Replace the “63” with however many characters you wish to remove.
        I also changed the file type to mp4 here so change it back of you wish to do other files

  4. arun Avatar
    arun

    What we do if want to remove from right side characters and rename file

  5. Annie Avatar
    Annie

    Instead of renaming based on # characters you want to remove, is there a way to do it based on everything preceding a particular character?

    For example, I want to remove all characters proceeding the underscore
    1_filename
    2_filenameX
    10_filenameY

    End result
    _filename
    _filenameX
    _filenameY

    1. Deepak K Avatar

      Yes, that would be even better but I haven’t figured that out yet.

      Does anyone else have any idea how to do it? 👆

  6. Sean Wilson Avatar
    Sean Wilson

    Thank you, this worked well for me. I renamed over 3000 files at once this way.

    1. DeepakNess Avatar

      Glad it worked for you.

  7. Joseph Nichols Avatar
    Joseph Nichols

    Is there a similar way of doing this for folders?

  8. Barry Smith Avatar
    Barry Smith

    Super helpful! Adding to my repertoire of helpful time s-saving skills. Should probably look into learning how to use PowerShell a lot more!

    1. DeepakNess Avatar

      Glad it helped, Barry. Definitely, PowerShell is amazing.

  9. LuisPuma Avatar
    LuisPuma

    Hi There,
    I used the same script you shared previously, and I found out that to be able to maintain characters in the middle you just have to know the start position and the end position, in my case, I had 300 files starting like XXXXXXXXX20210908YYYYYYYYY, I wanted to rename them as the date only, so I started in the position 9 and kept 8 characters, the result was that the new name files were only dates. You can apply the same principle to erase the last characters by starting in the 0 position.

    get-childitem *.txt | rename-item -newname {[string]($_.name).substring(9,8)}

    I hope this explanation works well for you.

    thanks

    1. DeepakNess Avatar

      That’s great! Thanks for sharing, LuisPuma.

  10. Albert J. Avatar
    Albert J.

    Great, many thanks 👍👍

  11. gnevius Avatar
    gnevius

    This variation will trim your filename (PDF in this case to 17 characters) while adding the extension.

    get-childitem *.pdf | rename-item -newname {[string]($_.name).substring(0,17) + $_.Extension }

  12. Pravallika Avatar
    Pravallika

    Hi,

    If I want to remove the required number of characters from a multiple folder, how can we do that?

    I used below command. but its not working. Please help me on this
    get-childitem *.dir | rename-item -newname { [string]($_.name).substring(1) }

  13. Johnathan Avatar
    Johnathan

    Is there a command that will allow me to do the but instead of the first 5 characters I can remove the last five characters?

    Example:

    Original file name: Apple_12345.pdf

    Want to change file name to: Apple.pdf

    1. DeepakNess Avatar
      DeepakNess

      Hi Johnathan,

      Yes, it works. I just wrote another post on how to remove the last few characters from multiple files that you can check out.

      Hope it helps. Thanks.

  14. John Silver Avatar
    John Silver

    Hey is there a way I can do this for multiple folders in a folder at once? to explain a long time ago back when ipods were still huge i had to change computers and used the ipod as the music harddrive and pulled all the music off the ipod harddrive but they all ended up with a .filename.(insert music type) so it’s been years at this point but is there a way to select the music folder and affect the subfolders within it?

    1. DeepakNess Avatar
      DeepakNess

      Hey John, I understood your use case but haven’t figured out a way to rename files in multiple folders at once. As of now, you will have to do this for each folder one by one.

    2. Jonathan Avatar
      Jonathan

      From what I understand, couldn’t you use a combination of cmd and this script to mass rename files via echo?

      1. DeepakNess Avatar
        DeepakNess

        Haven’t tried that! Did it work for you?

  15. Calico Avatar
    Calico

    Thanks so much, you just saved me so much time!! The instructions were super easy to follow even for someone like me, who isn’t very well-versed in stuff like this 🙂

    1. DeepakNess Avatar
      DeepakNess

      Glad it helped, Calico.

  16. Scott Avatar
    Scott

    Thank you! Most helpful article I’ve found so far on this.

    I’m trying to remove a single character that appears in every file. I’m trying to remove the “.” That appears in ‘Wausau.JFK”

    What code would work for this?

    Thanks so much!

    1. DeepakNess Avatar
      DeepakNess

      Hey Scott,

      get-childitem *.JFK | rename-item -newname { ($_.Name -replace '[.]', '') }

      The above command can help you remove the “.” from all the files with “JFK” extension. But make sure you have a copy of all the files before you run the command because the process is irreversible.

      Let me know if it helps.

  17. thomas Avatar
    thomas

    Saved me a couple hours!!

    Thank you Deepak! Appreciate Ya

  18. chintan Avatar
    chintan

    it is life saving👍

    1. DeepakNess Avatar
      DeepakNess

      Glad it helped you. 🤩

  19. Tbizz Avatar
    Tbizz

    Pretty slick!

    1. Deepak Avatar
      Deepak

      Glad it helped.

  20. Arjuunan Avatar
    Arjuunan

    THANK YOUUUUU!

    1. Deepak Avatar
      Deepak

      Glad it helped!

  21. Harshal Avatar
    Harshal

    Thank you it worked

  22. Kira Avatar
    Kira

    Great! Thank you so much!

  23. Brockney Avatar
    Brockney

    Pure genius with that, many thanks!

Leave a Reply to DeepakNess Cancel reply

Your email address will not be published. Required fields are marked *