Rename files to lowercase in Powershell

Keep in mind that you can pipe directly to Rename-Item and use Scriptblocks with the -NewName parameter (because it also accepts pipeline input) to simplify this task:

Get-ChildItem -r | Where {!$_.PSIsContainer} | 
                   Rename-Item -NewName {$_.FullName.ToLower()}

and with aliases:

gci -r | ?{!$_.PSIsContainer} | rni -New {$_.FullName.ToLower()}

Even though you have already posted your own answer, here is a variation:

dir Leaflets -r | % { if ($_.Name -cne $_.Name.ToLower()) { ren $_.FullName $_.Name.ToLower() } }

Some points:

  • dir is an alias for Get-ChildItem (and -r is short for -Recurse).
  • % is an alias for ForEach-Object.
  • -cne is a case-sensitive comparison. -ne ignores case differences.
  • $_ is how you reference the current item in the ForEach-Object loop.
  • ren is an alias for Rename-Item.
  • FullName is probably preferred as it ensures you will be touching the right file.

If you wanted to excludes directories from being renamed, you could include something like:

if ((! $_.IsPsContainer) -and $_.Name -cne $_.Name.ToLower()) { ... }

Hopefully this is helpful in continuing to learn and explore PowerShell.


slight tweak on this, if you only want to update the names of files of a particular type try this:

get-childitem *.jpg | foreach { if ($_.Name -cne $_.Name.ToLower()) { ren $_.FullName $_.Name.ToLower() } }

this will only lowercase the jpg files within your folder and ignore the rest