Powershell script to rename files

powershell, powershell-3.0

Solution

dir c:\tmp | % {
     mv $_.FullName $(Join-Path $_.Directory ($_.Name -replace "^([0-9\-\s]*)",'').Trim());
}

If you need to recursively process YourDirectory, add `-recurse` after `dir`.

Problem

I have a bunch of files that have names that start with numbers, may contain spaces and dashes after one of two numbers, then sometimes (sometimes not) have a space just before the first alpha character of the name I want to keep, i.e.: 2-01 Dazed And Confused.txt (I want to rename this to Dazed And Confused.txt) Or 02 - Uncle Salty.txt (I want to rename this to Uncle Salty.txt) Or 02-The Night Before.txt (I want to rename to The Night Before.txt)

Original source