PowerShell batch to file add prefix instance _powershell

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.