Get-ChildItem -Path C: emp est -Filter *.txt |
Rename-Item -NewName {$_.Basename.Replace("Old","New") + $_.extension} -WhatIf -Verbose
如上 將指定目錄下的所有的文件名從 old 改成 new..

adding -WhatIf at the end of the command we are saying to PowerShell: 'Just test, don't actually make any permanent changes'.

創(chuàng)新互聯(lián)建站是一家專業(yè)提供
阿拉山口企業(yè)網(wǎng)站建設(shè),專注與網(wǎng)站制作、
成都網(wǎng)站設(shè)計、
H5技術(shù)、小程序制作等業(yè)務(wù)。10年已為阿拉山口眾多企業(yè)、政府機構(gòu)等服務(wù)。創(chuàng)新互聯(lián)專業(yè)的建站公司優(yōu)惠進行中。
-Recurse 查子文件夾。
重命名示例
需求:將D盤For PS文件夾下的A.txt文件重命名爲aa.txt
- rename-Item 'D:For PSA.txt' -NewName 'aa.txt'
批量改文件擴展名
需求:將D盤For PS文件夾下的所有的txt文件改爲html文件,即.txt改爲.html
- get-childItem 'D:For PS' *.txt | rename-item -newname { $_.name -replace '.txt','.html' }
備註:由於replace的模式匹配字符串參數(shù)支持正則表達式,'.txt'要轉(zhuǎn)義成'.txt'。
批量爲文件加前綴
需求:將D盤For PS文件夾下的所有的txt文件加上一個“Test_”的前綴
- cd 'D:For PS'
- get-childItem -r *.txt | rename-Item -newname{'Test_'+$_.name}
如果覺得上面的命令太精簡,看不太懂,可以用如下語句,更好理解些:
- $dir = dir D:ForPS *.txt
- foreach($_ in $dir)
- {
- rename-Item $_.FullName -NewName ('Test_'+$_.Name)
- }
將D盤For PS文件夾下的所有的txt文件重命名為 Note1.txt、Note2.txt這樣的形式
- get-childItem 'D:For PS' -r *.txt | foreach-Object -Begin {$count = 1} -Process{
- rename-Item $_.fullname -NewName "Note$count.txt";$count++}
網(wǎng)頁題目:(PowerShell)重命名文件-創(chuàng)新互聯(lián)
文章網(wǎng)址:
http://weahome.cn/article/doesge.html