powershellcmduppercaselowercasetitle-case

Capitalize the first letter of each word in a filename with powershell


I want to change the names of some files automatically.

With this code I change the lowercase letters to uppercase:

get-childitem *.mp3 | foreach { if ($.Name -cne $.Name.ToUpper()) { ren $.FullName $.Name.ToUpper() } }

But I only want the first letter of each word to be uppercase.


Solution

  • You can use ToTitleCase Method:

    $TextInfo = (Get-Culture).TextInfo
    $TextInfo.ToTitleCase("one two three")
    

    outputs

    One Two Three

    $TextInfo = (Get-Culture).TextInfo
    get-childitem *.mp3 | foreach { $NewName = $TextInfo.ToTitleCase($_); ren $_.FullName $NewName }