Hey everyone! I recently recovered some files from a backup and wanted to clean them up a bit by deleting certain characters using a few scripts. Most of them worked fine, but one of the scripts caused extra parentheses to show up at the end of the file names, which I didn't intend. Here's the problematic script I used:
gci -Filter "*(2024_06_18 22_46_13 UTC)*" -Recurse | Rename-Item -NewName {$_.name -replace '(2024_06_18 22_46_13 UTC)', '' }
Basically, I wanted to remove the text "2024_06_18 22_46_13 UTC" from the file names, but now I'm stuck with these extra characters. Can someone help me figure out how to remove those pesky parentheses in bulk? Or better yet, is there a way to fix my script so that this issue doesn't happen again? Thanks a bunch!
3 Answers
To fix your script and avoid the parentheses issue, try using this:
$_.Name -replace '(2024_06_18 22_46_13 UTC)', ''
In regex, parentheses are special characters, so you'll need to escape them with a backslash to get them to work right. Alternatively, you can use the .Replace() method like this:
$_.Name.Replace('(2024_06_18 22_46_13 UTC)', '')
This method doesn't require escaping since it treats the text literally. It should help you get the results you want without the extra characters!
Gotcha! That explains why I had issues with -replace. I’ll try the .Replace() method instead. Thanks for the tips!
Make sure your script doesn't have issues with text formatting. Using code blocks helps avoid strange errors. Here’s the line you're trying to adjust, formatted:
gci -Filter "*(2024_06_18 22_46_13 UTC)*" -Recurse | Rename-Item -NewName {$_.name -replace '(2024_06_18 22_46_13 UTC)', '' }
This should look better when you enter it into PowerShell!
It looks like there may have been some hiccups with the text formatting in your code. To ensure everything runs smoothly, try using the code block correctly. Just add 4 spaces before your lines:
gci -Filter "*(2024_06_18 22_46_13 UTC)*" -Recurse | Rename-Item -NewName {$_.Name -replace '(2024_06_18 22_46_13 UTC)', '' }
This format should help clarify things when you run your script.

So if I rework it like this:
gci -Filter "*(2024_06_18 22_46_13 UTC)*" -Recurse | Rename-Item -NewName {$_.Name.Replace('(2024_06_18 22_46_13 UTC)', '')}
Would that be correct?