Rename a bunch of files/directories

This has been quite useful to me as well.

Files:
get-childitem “*.xml” | foreach { rename-item $_ $_.name.Replace(“NES-“, “NES-X”) }Replace

Directories:
Get-ChildItem -path PROD -Directory | ForEach-Object -Process {Rename-item -path $_.Name -NewName ($_.name.substring(4,8)) -Verbose }
Another example:
Get-ChildItem ‘C:\Temp\crs\documents’ | rename-item -newname { $_.name.substring(0,45) + ‘_Client_Report.pdf’}

Here is a variant to add a string to an existing file name:
get-childitem “*.pdf” | foreach { rename-item $_ $_.name.Replace($_.Basename,”LU0010001_2016_2016_08_22_14_33_17_277_” + $_.Basename) }

Leave Comment

Your email address will not be published. Required fields are marked *