I'm trying to rename the following files in a batch:
image.part-Frag1 image.part-Frag10 image.part-Frag11 > Get-ChildItem *frag* | Rename-Item -NewName "$($_.Name).png" ... but am getting the following error:
Rename-Item : Cannot create a file when that file already exists. At line:1 char:24 + Get-ChildItem *frag* | Rename-Item -NewName "$($_.Name).png" + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : WriteError: (C:\Users\user\Des...html.part-Frag1:String) [Rename-Item], IOException + FullyQualifiedErrorId : RenameItemIOError,Microsoft.PowerShell.Commands.RenameItemCommand Rename-Item : Cannot create a file when that file already exists. At line:1 char:24 + Get-ChildItem *frag* | Rename-Item -NewName "$($_.Name).png" + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : WriteError: (C:\Users\user\Des...tml.part-Frag10:String) [Rename-Item], IOException + FullyQualifiedErrorId : RenameItemIOError,Microsoft.PowerShell.Commands.RenameItemCommand Rename-Item : Cannot create a file when that file already exists. At line:1 char:24 + Get-ChildItem *frag* | Rename-Item -NewName "$($_.Name).png" ...
$_variable (whose alias is$PSItem) is only ever meaningfully defined inside script blocks ({ ... }), such as inForEach-ObjectandWhere-Objectcalls, calculated properties, and delay-bind script blocks, which are required in your case. See this answer for details.