# Batch rename script

**URL:** <https://forums.powershell.org/t/batch-rename-script/8805>\
**Category:** PowerShell Help\
**Created:** [June 10, 2017, 6:33pm UTC](https://forums.powershell.org/t/batch-rename-script/8805 "2017-06-10T18:33:55Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![afosnot](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/afosnot/32/480_2.png) [@afosnot](https://forums.powershell.org/u/afosnot)\
**Post date:** [June 10, 2017, 6:33pm UTC](https://forums.powershell.org/t/batch-rename-script/8805/1 "2017-06-10T18:33:55Z")

</div>

Hello all,

I’m working on a script to rename a large amount of files my team has mislabeled.

Currently every folder has in example the following.  
ls/dir outputs:  
0.pdf  
00.pdf (file needs to be first)  
1.pdf  
2.pdf  
3.pdf  
4.pdf

When running: ($nr=1)  
Dir | %{Rename-Item $\_ -NewName (‘{0}.pdf’ -f $nr++)}

The output is as follows:

After rename  
1.pdf  
2.pdf (Cover sheet file)  
3.pdf  
4.pdf  
5.pdf  
6.pdf

I am pretty new to Power Shell and honestly haven’t used a CLI in quite some time so I am curious on a method that will sort the 00.pdf to the top and when rename is ran it’ll output it to 1.pdf.

---

<div class="post-metadata">

**Author:** ![liam-kemp](https://avatars.discourse-cdn.com/v4/letter/l/48db29/32.png) [@liam-kemp](https://forums.powershell.org/u/liam-kemp)\
**Post date:** [June 10, 2017, 7:34pm UTC](https://forums.powershell.org/t/batch-rename-script/8805/2 "2017-06-10T19:34:54Z")

</div>

Im not sure if there is a way to sort that way. Someone else here may know of a solution.

If the contents of each folder are always the same order, one method I can think of would be

Step 1 is fine the way you have it.  
Add a second step to rename 2.pdf to 0.pdf  
Add a third step that is the same as the first

Liam

---

<div class="post-metadata">

**Author:** ![chris-bakker](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/chris-bakker/32/6356_2.png) [@chris-bakker](https://forums.powershell.org/u/chris-bakker)\
**Post date:** [June 11, 2017, 2:08am UTC](https://forums.powershell.org/t/batch-rename-script/8805/3 "2017-06-11T02:08:17Z")

</div>

This will sort it for you.

Get-ChildItem \*|Sort-Object -Property name

But in this way, you get in trouble  
The 0.pdf wil come before 00.pdf  
You can use other properties to sort, like access time to.

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [June 11, 2017, 3:54am UTC](https://forums.powershell.org/t/batch-rename-script/8805/4 "2017-06-11T03:54:03Z")

</div>

It might not be the most sophisticated or elegant way but I guess it does the job:

```
$Directory = ‘Your Directory with misslabeld files here’
$Counter = 2
Get-ChildItem -Path $Directory -Filter ?.pdf |
Sort-Object |
ForEach-Object {
Rename-Item -Path $_.FullName -NewName (‘#####’ + $Counter + $_.Extension)
$Counter++
}
Get-ChildItem -Path $Directory -Filter ‘00.pdf’ | Rename-Item -NewName (‘#####1.pdf’)
Get-ChildItem -Path $Directory -Filter ‘#####?.pdf’ |
Sort-Object |
ForEach-Object {
Rename-Item -Path $_.FullName -NewName (($_.BaseName -replace ‘#####’) + $_.Extension)
}
```
 … and BTW: if you don’t need this on a regular base it would have bin easier to take a file rename batch tool like it is included in some file management tools like FreeCommander

---

<div class="post-metadata">

**Author:** ![wilkster](https://avatars.discourse-cdn.com/v4/letter/w/e68b1a/32.png) [@wilkster](https://forums.powershell.org/u/wilkster)\
**Post date:** [July 3, 2017, 4:47pm UTC](https://forums.powershell.org/t/batch-rename-script/8805/5 "2017-07-03T16:47:54Z")

</div>

try Krojam Soft Batch Rename program

---

<div class="post-metadata">

**Author:** ![js2010](https://avatars.discourse-cdn.com/v4/letter/j/ebca7d/32.png) [@js2010](https://forums.powershell.org/u/js2010)\
**Post date:** [July 4, 2017, 10:12am UTC](https://forums.powershell.org/t/batch-rename-script/8805/6 "2017-07-04T10:12:27Z")

</div>

I guess you can first:

```
mv 00.pdf -1.pdf
```

but won’t there be errors because often the new filename already exists (unless you sent the results to another folder or do it backwards)?

Another way to do it with a script block:

```
Dir | Rename-Item -NewName { "$([int]$_.basename + 1).pdf" }
```

_Apparently, any parameter that can take pipe input, can accept a script block._

```
echo hi | echo -inputobject { $_ }

hi
```

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:38pm UTC](https://forums.powershell.org/t/batch-rename-script/8805/7 "2024-05-16T20:38:25Z")

</div>


