Let's assume that you have a lot of script files (or pictures, logs, and so on) in the folder and you want to rename them all. The resulting new filename should have a number such as a prefix.
This will show you how to achieve it.
The example will rename the PowerShell script in the folder you specified for all the extensions named. PS1. The new name will become Powershellscriptx.ps1, where the "X" is an incrementing number.
Note Script defaults have not really started renaming. Please be careful to remove its-whatif parameter to really rename the file. Suppose you input a variable or enter a wrong directory path, then your script will mistakenly rename thousands of files, which you do not want to see.
Copy Code code as follows:
$Path = ' C:\Temp '
$Filter = ' *.ps1 '
$Prefix = ' Powershellscript '
$Counter = 1
Get-childitem-path $Path-filter $Filter-recurse |
Rename-item-newname {
$extension = [System.io.path]::getextension ($_. Name)
' {0}{1}. {2} '-F $Prefix, $script: Counter, $extension
$script: counter++
}-whatif
Experience:
Common string handling methods, focusing on ' {0}{1} '. {2} '-F usage.
Article Source: http://www.pstips.net/bulk-file-renaming.html