-5

I need a Powershell script that will find and replace uppercase characters within all the filenames within a directory with nothing.

The script would do something like this:

Filename BEFORE: CAPITAL-letters.jpg

Filename AFTER: -letters.jpg

Edit: I tried running this script, but it didn't work

dir | rename-item -NewName {$_.name -replace [Regex]::Escape("[A-Z"),""}
abednabir
  • 33
  • 5

2 Answers2

2
$test = "CAPITAL-letters.jpg"
$test -creplace "[A-Z]"

=> -letters.jpg

Manu
  • 1,685
  • 11
  • 27
1

since this got answers, for full clarity:

Remove -WhatIf when you're running it.

Get-ChildItem | Rename-Item -NewName {$_.Name -creplace "[A-Z]"} -WhatIf

This pipes Get-ChildItem into Rename-Item and then Sets the NewName to be the old name, except we've case-sensitively replaced any capital letters with nothing.

colsw
  • 3,216
  • 1
  • 14
  • 28
  • What if I want to run this script with an entire directory? – abednabir Jun 08 '17 at 12:45
  • @abednabir i'm sure you've figure dit out already - but running this will perform the action on the entire current folder, you can add `-Recurse` to the `Get-ChildItem` command to have it include subfolders. – colsw Jun 08 '17 at 14:20
  • My apologies - I was in a bit of a hurry when I left that comment. The script does work for file directories. – abednabir Jun 09 '17 at 13:52