3

I'm running this PowerShell command:

Get-ChildItem .\tx\*.htm | Rename-Item -NewName {$_.Name -replace '\.htm','.tmp'}

and receive the following error when a filename contains square brackets -- [ and/or ] --, understandable since those have a meaning within the PowerShell syntax.

Rename-Item : Cannot rename because item at
'Microsoft.PowerShell.Core\FileSystem::C:\users\xxxxx\desktop\tx\
Foofoofoofoo_foo_foo_[BAR]_Foofoofoofoo_foofoofoo.htm' does not exist.
At C:\users\xxxxx\desktop\foo002.ps1:59 char:39
+ Get-ChildItem .\tx\*.htm | Rename-Item <<<<  -NewName { $_.Name -replace '\.htm','.tmp' }
    + CategoryInfo          : InvalidOperation: (:) [Rename-Item], PSInvalidOperationException
    + FullyQualifiedErrorId : InvalidOperation,Microsoft.PowerShell.Commands.RenameItemCommand

All the other files in the path have similar names (Phrase_With_Underscores.htm), and are renamed without incident. Anyone have any experience with this and know how to counter it so I can manipulate these files?

Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
dwwilson66
  • 6,806
  • 27
  • 72
  • 117

1 Answers1

7

You can workaround using move-item for renaming items using the parameter -LiteralPath.

Is a know bug: Connect (read Keith Hill Comment for: powershell V3 fix this issue)

CB.
  • 58,865
  • 9
  • 159
  • 159