Powershell, rename-item doesn’t work as expected


I have a bunch of jpg image files named in the following pattern:

I want to rename them by removing the first 5 characters to get the form:


I use the following command:

When using this with -whatif flag i get the expected message saying:

But removing the whatif gives me errors of this type:

followed by a whole bunch of:

The files themselves are renamed with random number of characters removed rather than 5 as was intended. So they have ended up like:


I have used this command to rename such files in the past with success. The fact that I’m getting such random results is making me pull my hair out.



Make sure you only process the files of interest:

The -WhatIf common parameter in the command above previews the operation. Remove -WhatIf once you’re sure the operation will do what you want.
In PowerShell [Core] 6+, placing (...) around Get-ChildItem is no longer technically necessary, but advisable.[1]

That way:

  • You rule out unrelated files up front.
  • Even if something goes wrong, you can correct the problem and run the command again to reprocess only the failed files, without affecting the previously renamed files.
    • The most likely reason for something going wrong is more than 1 input file resulting in the same filename after removing the 5 first char.

It sounds like you’ve mistakenly run the command repeatedly, so you’ve cut off 5 chars. multiple times:

0001-rand01_01.jpg -> rand01_01.jpg -> _01.jpg

Once a filename has fewer than 5 chars., you’ll get the the startIndex-related error, because the [string] class’s .Substring() method doesn’t accept an index beyond the length of the string (try 'ab'.Substring(3)).

That said, since you’re running Get-ChildItem without a filter and therefore return all (non-hidden) child items, you may be processing unrelated files ore even directories whose names are too short.

The Cannot create a file when that file already exists. errors are just follow-on errors that result from the script block that normally returns the new name effectively returning the empty string, so Rename-Item is somewhat obscurely complaining that you can’t rename a file to its current name.

That said, you can even get Cannot create a file when that file already exists errors during the first run, namely if more than 1 input file with its first 5 chars. chopped off results in the same filename.

E.g., 0001-rand01_012.jpg and 0002-rand01_012.jpg would both be renamed to rand01_012.jpg, which fails once the first one has been renamed.

That is, for your command to work as intended, all filenames that result from dropping the first 5 chars. must be unique.

Here’s an MCVE (Minimal, Complete, and Verifiable Example):


1st run:

A 2nd run yields:

At the time of the 3rd run, all the names are too short, and all you’ll get is Rename-Item : Cannot create a file when that file already exists. errors.

[1] Enclosing Get-ChildItem in (...) ensures that the matching files are collected in an array, up front, before Rename-Item is invoked.
This explicitly prevents already-renamed files from getting re-enumerated by Get-ChildItem and thus interfering with the iteration. Explicit use of (...) is technically no longer necessary in PowerShell [Core] 6+ (it is necessary in Windows PowerShell (5.1-)), because Get-ChildItem is implemented in a way that always internally collects info about all files up front, across platforms, because it sorts them by name, which is inherently only possible after all names have been collected.
In light of that, whether you use (...) or not should functionally amount to the same, although using (...) is advisable, because it doesn’t rely on what amounts to an implementation detail (the documentation doesn’t mention how the outputs are ordered).


Powershell, rename-item doesn’t work as expected by licensed under CC BY-SA | With most appropriate answer!

Leave a Reply