Search code examples
powershellbatch-rename

bulk rename using powershell to get rid of 9 characters that follow a "-"


I need to rename around 5000 files that have the following naming format:

"322xxx-710yy.tiff"

i need to remove -710yy from the name to get 322xxxx.tiff

can anyone suggest a rename-item command for power-shell to do this?

i tried:

    get-childitem -filter "322*" -recurse | Rename-item -NewName{$_.name -replace '-710*', ''}

but all that does is remove the "710" leaving the remaining characters after it.


Solution

  • -replace takes a regex, not a wildcard match (unlike the -Filter parameter of Get-ChildItem), so you need

    $_.Name -replace '-710[^.]*'
    

    instead (you can also leave out the , '' in that case, as it's implied). Note that I'm only replacing non-dot characters after the 710, to keep the extension intact.