Skip to content

Rename (trim) multiple file names

PowerShell command

Use a simple PowerShell command combined with regular expressions (RegEx). Without or with a different file extension you have to adapt the back part for your needs.

ls | Rename-Item -NewName {$_ -replace '(_\d+_\d+)\.txt', '.txt' }

Explanation RegEx:

_ matches the character _ 
\d matches a digit (equivalent to [0-9])
+ matches the previous token between one and unlimited times
_ matches the character _ 
\. matches the character . 
txt matches the characters txt literally (case sensitive)

Use the -WhatIf parameter on Rename-Item to check renames in order to avoid a mistake.

ls | Rename-Item -NewName {$_ -replace '(_\d+_\d+)\.txt', '.txt' } -WhatIf

PowerShell rename sample

Further solutions with graphical user interface

Bulk Rename Utility allows you to easily rename files and entire folders based upon extremely flexible criteria.

Ant Renamer is another free program that makes easier the renaming of lots of files and folders by using specified settings. It supports Unicode names.