5

I restored a large number of files in diverse folders from a hard drive, and they all included a string.

I removed this string in windows powershell using

get-childItem -recurse | Where {$_.name -like "*(2025_09_29 13_23_14 UTC)*"} | rename-item -newname {  $_.name  -replace "(2025_09_29 13_23_14 UTC)",""  }

but was left with all files now being named filename().file. I can't remove the "()" with the same script for some reason.

get-childItem -recurse | Where {$_.name -like "*()*"} | rename-item -newname {  $_.name  -replace "()",""  }

This command does not change my file names, and I'd really like to get rid of this remaining () string. Does anyone know what is going on here?

1
  • 5
    Do -replace "\(\)". You need to escape parentheses when using regex and the escape character is \ Commented Oct 1 at 13:00

2 Answers 2

6

The reason you have a leftover () is due to using -replace (regex replacement operator) and parentheses being a capturing group in regular expressions. To match them literally you should've escaped them using \, see Escaping characters in the doc linked before.

Get-ChildItem -Recurse |
    Where-Object { $_.Name -like '*(2025_09_29 13_23_14 UTC)*' } |
    Rename-Item -NewName { $_.Name -replace '\(2025_09_29 13_23_14 UTC\)' }

Now, to fix your problem you can use the same approach as you have but escaping them:

Get-ChildItem -Recurse |
    Where-Object { $_.Name -like '*()*' } |
    Rename-Item -NewName { $_.Name -replace '\(\)' }

Alternatively, you can use string.Replace which is always literal:

Get-ChildItem -Recurse |
    Where-Object { $_.Name -like '*()*' } |
    Rename-Item -NewName { $_.Name.Replace('()', '') }
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for explaining, i've tried using escaping instead and it works perfectly!
Glad it helped!
1

As mentioned already -replace uses regex to find and replace strings.

What fits better your needs is just regular replacement that can be achieved with Replace string function. Please see below:

"Hello World!".Replace("Hello", "Hi")

would print Hi World!.

In your case that would look like:

$_.name.Replace("(2025_09_29 13_23_14 UTC)","")

That would replace also () and won't left them behind.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.