2

I have bunch of files containing + sign in their name this script runs for removing any substring and character when doesn't work for '+' sign

Get-ChildItem -Recurse '*.*' | Rename-Item -NewName { $_.Name -Replace '+','' } Please help

I tried to remove '+' sign from filenames

shasabbir
  • 80
  • 9
  • 1
    PowerShell's `-replace` operator is [regex](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_Regular_Expressions)-based. Regex _metacharacters_ (those with special meaning), namely `\ ( ) | . * + ? ^ $ [ {`, need to be ``\``-_escaped_ in order to be used _verbatim_; escape whole strings with `[regex]::Escape()`. Alternatively, use the `[string]` type's `.Replace()` method for verbatim replacements, but note that it is case-_sensitive_, _invariably_ in Windows PowerShell, and _by default_ in PowerShell (Core) 6+. See the linked duplicate for details. – mklement0 Feb 12 '23 at 23:37

1 Answers1

2

+ sign has a special meaning in regular expressions, if you want to match it literally using -replace, the same needs to be escaped:

Get-ChildItem -Recurse '*+*.*' |
    Rename-Item -NewName { $_.Name -Replace '\+' }

Or use a literal replacement method instead:

Get-ChildItem -Recurse '*+*.*' |
    Rename-Item -NewName { $_.Name.Replace('+', '') }
Santiago Squarzon
  • 41,465
  • 5
  • 14
  • 37