Search code examples
powershellfile-renamebatch-rename

Capitalize First Letter Of Each Word In Multiple Filenames (Powershell)


I have many folders with many files in each that I want to capitalize the first letter of each word for in a batch. I've seen solutions for capitalizing strings, but that's not what I'm after. I use Powershell and I like to copy the script into the active folder and just run it. Therefore, most of my PS scripts begin with:

$PSScriptRoot

Example to rename files:

$PSScriptRoot
Get-ChildItem -File | Rename-Item -NewName { $_.Name -Replace "OLD","NEW"}

So just looking for something similar that capitalizes the first letter of each word in ALL filenames (excluding the extensions of course) in a batch.


Solution

  • Something like this?

    Get-ChildItem -File | Rename-Item -NewName { "{0}{1}" -f ( Get-Culture ).TextInfo.ToTitleCase( $_.BaseName.ToLower() ), $_.Extension}
    

    for better performance you can rename only file with difference name like this :

    Get-ChildItem -File | select *, @{N="NewName";E={"{0}{1}" -f ( Get-Culture ).TextInfo.ToTitleCase( $_.BaseName.ToLower() ), $_.Extension}} | 
    where {$_.Name -CNE $_.NewName} | Rename-Item -NewName {$_.NewName}