Find characters and rename file name using Powershell Find characters and rename file name using Powershell powershell powershell

Find characters and rename file name using Powershell


Here's an example to rename all pdf files in the current directory, removing all leading numbers or underscores from the file name:

Get-ChildItem -Filter *.pdf | Rename-Item -NewName {$_.Name -replace '^[0-9_]+'}


Here's another method that doesn't rely on regex but assumes no underscores in the filepath:

Get-ChildItem 'C:\path2files' | %{    Rename-Item $_.fullname $_.Name.Split("_")[2]    }


Given a string like "1050_14447_Letter Extension.pdf", here is one way:

$foo = "1050_14447_Letter Extension.pdf"$foo -replace '^.*_.*_(.*)$', '$1'