how to use PowerShell to rename multiple files at once?

This topic contains 5 replies, has 5 voices, and was last updated by  Rick 8 months, 3 weeks ago.

  • Author
  • #35237

    John Chen

    Basically I have thousands of files. I rename first file as "file0001.txt" for example. Now I want the rest of files to be renamed as "file0002.txt", "file0003.txt", etc. How can I achieve that using PowerShell? Thanks a lot!

  • #35238

    Flynn Bundy

    Hi John

    You will want to use a foreach loop to iterate through all the files in your directory. Here is an example:

    get-childitem D:\Stuff\ -File | ForEach-Object {Rename-Item -Path $_.FullName -NewName "NewName$(Get-Random).txt"}

    Doing this allows us the pass all the files in D:\Stuff to the Rename-Item Cmdlet. The rename-Item cmdlet needs to know exactly what it's renaming so we use the FullName property from Get-Childitem to do this.

    It's important to note that in the above example this would rename ALL files to this new standard. If you wanted just text files to be renamed you would use a filter on your Get-Childitem cmdlet to only gather these before piping them along. Also, the use of Get-Random here is to just for files since they require a unique name.

    if you wanted an example where the files are named in order you could use something like this, also this example will filter for only .txt files to be renamed:

    $Files = gci D:\Stuff\ -Filter *.txt -file
    for ($i = 0; $i -lt $Files.count; $i++){
    Rename-Item -Path $Files[$i].FullName -NewName "NewName_$i.txt"

    Let us know if you require any further help.

  • #35249

    Liam Kemp

    Hi John,
    If you want to get something similar to "file0001.txt" etc, try

    $files = get-childitem C:\myfiles -recurse | Where { !($_.PSIsContainer) }
    $count = 0
    foreach($file in $files){
    If ($count -lt 10)
    	Rename-Item -path $file.FullName -NewName "file000$count"
    elseif ($count -lt 100)
    	Rename-Item -Path $file.FullName -NewName "file00$count"

    I'll walk you through what is happening
    First I'm getting the list of files, in the top folder, and sub folders, and sticking them in a variable. I'm excluding the actual folder objects, if we start renaming those, the whole thing will break.
    Then I create a variable to store the current count, starting at 0.

    Then for every file that we stored earlier, we're going to rename it. If $count is less than 10, i.e. is a single digit number, we'll append "000$count" to the end of our new file name. Then while $count is less than 100 (2 digit number), we stick "00$count" on the end. Obviously, if you want to go all the way to say 9999, you will need to add a couple more "if's", but this will allow you to name the files from 0000 upwards.

    Flynn's method will work, and is much shorter, but I think the files will be named
    etc – They won't keep the standardised name/length you seem to be after. I could be wrong.

  • #35284

    John Chen

    Flynn and Liam, thanks so much for your help. I just changed a little bit on your codes to fit my need and it works perfectly!

  • #80588


    Using PowerShell is very difficul, there is a simple solution for your trouble. I suggest you to try BatchRenameFiles Tool program You can easily found hier

  • #80593


    Try this:

    $i = 0
    gci C:\temp | Where { !($_.PSIsContainer) } | % {
       $i += 1
       Rename-item $_.FullName -NewName ((Split-path $_.FullName -Parent) + ("\file" + ([string]$i).Padleft(4,"0") + $_.Extension )) -WhatIf

You must be logged in to reply to this topic.