Search code examples
powershellfile-renameuppercasebatch-rename

rename multiple folder to Uppercase not files using powershell


I am working on powershell where I have multiple folders i need to change all folders names into Uppercase not the files only folder.

I have tried the below code

Get-ChildItem -Path "C:\Users\Xyz\Desktop\sample" -Recurse | % { 
  if ($_.Name -cne $_.Name.ToUpper()) { ren $_.FullName $_.Name.ToUpper() } 
}

But with this code it was changing only file name but I want to change only dir

for example

enter image description here

foldername (lowerCase)

abc
cab
dab

like this (UPPERCASE)

ABC
CAB
DAB

Thanks in advance


Solution

  • These tricks may not be obvious. How's this? Hmm, that didn't actually work. You can't rename folders to the same thing in upper case in powershell.

    # doesn't work!
    get-childitem -recurse -directory -path "C:\Users\Xyz\Desktop\sample" | 
      Rename-Item -NewName { $_.name.toupper() } -whatif
    

    Sometimes calling cmd from powershell just works better. Try this first as a "whatif" to see if it does what you want. And if I really understand the question. All this does is echo strings. This command is just "pretend".

    get-childitem -recurse -directory -path "C:\Users\Xyz\Desktop\sample" | 
      foreach { write-host cmd /c ren $_.fullname $_.name.toupper() }
    

    And if that looks good, this actually does the rename. But maybe make a backup in case something goes wrong. Be able to undo the action.

    get-childitem -recurse -directory -path "C:\Users\Xyz\Desktop\sample" | 
      foreach { cmd /c ren $_.fullname $_.name.toupper() }