Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PowerShell Rename file name and keep extension

Tags:

powershell

I have found code on here which does what I need it to it

$files = gci -filter "*.txt" |select fullname
foreach ($file in $files) {
   $filename = $file.fullname
   $newFilename = $filename.Replace(".", " ")
   rename-item $filename $newFilename
} 

I require the code to change the file name and keep the file extension. The code above replaces "." with " ". I can code it to rename the file again to add the extension back. Just wanted to see if there was a better way to do it.

Cheers

like image 553
User79.Net Avatar asked Sep 21 '15 04:09

User79.Net


1 Answers

Change 'old' & 'new' to whatever you want Remove the -Whatif to apply the command.

Get-ChildItem -Path C:\temp\files -Filter *.txt | 
 Rename-Item -NewName {$_.Basename.Replace("old","new") + $_.extension} -WhatIf -Verbose
like image 162
Kiran Avatar answered Sep 30 '22 21:09

Kiran