How do I rename each .jpg file name with consecutive numbers in PowerShell

Viewed 185

I'm completely new to PowerShell, or any shell for that matter. I'm trying to figure out a way to rename 109 photos that are marked IMG_3571 to IMG_3679. I want to number them consecutively starting at 236. I've tried a few things and this is where I am at right now:

Get-ChildItem "C:\Files to Transfer\test"*.jpg | ForEach-Object -begin {$count=236} -process {rename-item -Path "C:\Files to Transfer\test" -NewName "$count"}

I get this error message 108 times:

At line:1 char:95
+ ... } -process {rename-item -Path "C:\Files to Transfer\test" -NewName "$ ...
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [Rename-Item], PSInvalidOperationException
    + FullyQualifiedErrorId : InvalidOperation,Microsoft.PowerShell.Commands.RenameItemCommand

Also the file named "test" (where all the photos are contained) gets changed to 236...

Edit: I would like to have the files without "IMG" in the name. Only the numbers.

Thanks everyone! Here is what did it:

Get-ChildItem "C:\Files to Transfer\test\*.jpg" | ForEach-Object -begin {$count=236} -process {rename-item -Path $_.fullname -NewName "$count.jpg";$count++}

1 Answers

You are close.

You need $_ in the rename. $_ is the pipeline variable that Get-ChildItem is feeding. Your output file needs IMG- as a prefix And you need to increment count.

Try this:

Get-ChildItem "C:\Files to Transfer\test\*.jpg" | ForEach-Object -begin {$count=236} -process {rename-item -Path $_.fullname -NewName "$count.jpg";$count++}

It is very useful to add -WhatIf to things you are trying so that you can see what will happen without actually doing it.

Related