Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm attempting to rename multiple files that have filenames ending in the same way. These files are currently: "Foo Bar (U) (V7.9) (USA).txt" "Few Ber Doo Ber (U) (V7.9) (USA).txt" ect

I'm trying to remove the "(U) (V7.9) (USA)" section of the name and replace this section with (USA). These files would then be named: "Foo Bar (USA).txt" "Few Ber Doo Ber (USA).txt"

I can't quite get it to work, but here's what I've been trying to no avail:

get-childItem '* (U) (V7.9) (USA).txt' | rename-item -newname { $ -replace '(U) (V7.9) (USA)','(USA)' }
share|improve this question
up vote 5 down vote accepted

Several of the characters you are trying to replace are special characters in regex. They have special, reserved meanings that allow you to create complicated expressions when they are not "escaped." In order to escape them, you simply need to include a backslash infront of the special character. Example:

PS C:\> $string = "(U) (V7.9) (USA)"

PS C:\> $string -replace "(U) (V7.9) (USA)", "(USA)"
(U) (V7.9) (USA)

PS C:\> $string -replace "\(U\) \(V7\.9\) \(USA\)", "(USA)"

Here's a Powershell Regex Reference in case you run into similar problems. You may notice that the backtick ` character is used instead of the backslash -- the backtick is the escape character in powershell, but that is not true for all regex handling in .NET (which Powershell is built on). Because of that, you may see both used in regex examples for powershell on the internet.

share|improve this answer
Thanks for your help and sorry for the delayed post. I was having some difficulty with "$string -replace" for some reason, but here's what I used that works perfectly for me: get-childItem '*(U) (V7.9) (USA)*' | rename-item -newname { $ -replace '\(U\) \(V7.9\) \(USA\)','(USA)' } – root Sep 21 '13 at 14:13

If you just want to remove the "(U) (V7.9)" Part, you also could use:

-replace("\(U\) \(V7.9\) ","")

But you have to use the escapecharacter for the parantheses too. And dont forget the following spacecharacter, because we dont want to get a doublespace.

Also theres an option to use the substring(start,length) Method. But there you have to calculate the beginning of the "(U) (V7.9)" Part.

$string = "Randomname (U) (V7.9) (USA).txt"
$string.substring(0, $string.length - 20) + $string.substring($string.length - 9)

which outputs Randomname (USA).txt.

The 20 is the number of chars (beginning on back) to the first character of the string to remove (in this case: ( of (U)). The 9 is the number of chars (beginning on back) to the first character after the string to remove (in this case: ( of (USA), cause we dont want to get a double-space).

This method is independent of the Length of the Randomnamebut only works with known position and length of the string to remove. Anyways the -replace-Method is easier to use, if you know the escapechararcter. I just wanted to mention an other way to get the desired output.

share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .