I have a few hundred JPGs that need to be renamed. I need the following simple equation applied to the existing number in each file name:


"Vitrine Hinge.0.jpg" would become "Vitrine Hinge.290.jpg", "Vitrine Hinge.1.jpg" would become "Vitrine Hinge.289.jpg", and so on.

Some of the files in question

I have used the following:

Dir *.jpg | ` foreach { $data = $_.name.Split(".") Rename-Item -NewName { '(' + $data1 + '-290)(-1)' } }

And I receive errors.

  • Batch renaming files is a common question on SU. Have you researched for previous solutions? Are you working with code and are stuck with a specific problem? – root Jan 3 '18 at 14:53
  • @root I have researched. – L Wright Jan 3 '18 at 16:06
  • I have used this as a simple batch rename: Dir *.jpg | ForEach-Object -begin { $count=-146 } -process { rename-item $_ -NewName "VitrineHinge.$count.jpg"; $count-- }. But it has no regard for the order of the files, and rearranges the order. – L Wright Jan 3 '18 at 16:08
  • if the integer is always after the first . you can use this: gci . *.jpg | % { $int = $_.Name.Split(".")[1] ; $number = ($int - 290) * -1 ; rni $_.FullName -NewName ($_ -replace $int,$number) } – SimonS Jan 4 '18 at 9:39
  • Instead of (x-290)*-1, why not just say 290-x? – G-Man Says 'Reinstate Monica' Jan 5 '18 at 5:30

Ryan's answer is nice because it works without a hard reference to the base name of the new file name.

I'm always renaming files for one reason or another and found creative ways to manipulate strings using powershell. Here's another way to do this:

gci *.jpg | Rename-Item -NewName {$_.basename -replace "$($_.basename.split(".")[-1])$","$(290-$_.basename.split(".")[-1])$($_.extension)"}

This one-liner can be reused to rename other files in the same way, whether they're jpgs, gifs, or txt files. Only requirement is that the last part of the basename end with ".#"

So, what's going on here? To break it down, let's say $_ is currently a [System.IO.FileInfo] object for the file "Vitrine Hinge.12.jpg". Therefore:


would equal [string] "Vitrine Hinge.12"


would result in an array with the following entries:

Door Hinge

As such, [1] would refer to the "12". However, note that I instead used:


This is because if I had a file named, "Viterine Hinge.Chrome.12.jpg", I would have made reference to "Chrome". Using [-1] will give me the last item in the array and therefore it wouldn't matter if my file had other periods in the name.

OK, so now at this point, I encapsulate that in $() and use in in my -replace operator. Note that I end the first argument with $ so that I would regex match at the end of the string. This way, It wouldn't matter if my files had other numbers in the name. For example, "Viterine D00r Knob.0.jpg" would not get renamed to "Viterine D290290r Knob.290.jpg"

Let me know if missed something, Cheers :)


Something like this should work.

Get-ChildItem *.Jpg|%{$SplitName=$_.name -split '\.';$NewNumber= $(([int]$SplitName[1]-290)*-1);$NewName=@($SplitName[0],$NewNumber, $SplitName[2]) -join '.'; Rename-Item $_.name $Newname}

Broken Out it would be.

$Files=Get-ChildItem *.jpg
Foreach ($File in $files){
    $SplitName=$File.name -split '\.';
    $NewNumber= $(([int]$SplitName[1]-290)*-1)
    $NewName=@($SplitName[0],$NewNumber, $SplitName[2]) -join '.'
    Rename-Item $_.name $Newname
  • Thanks Ryan. A FB friend helped me out and came up with: gci *.jpg | foreach { $data = $_.name.Split("."); $index = ([int]$data[1]-290)*(-1); rni $_ " Vitrine Hinge.$index.jpg" } – L Wright Jan 4 '18 at 22:30
  • gci *.jpg | foreach { $data = $_.name.Split("."); $index = ([int]$data[1]-290)*(-1); rni $_ " Vitrine Hinge.$index.jpg" } – L Wright Jan 4 '18 at 22:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.