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've been doing quite a bit of searching/experimenting, and I know similar problems have been solved, but I'm struggling with this problem specifically and am reaching out for another pair of eyes. My goal is to parse an .xml file for names, alter the names, then save them. My .xml in brief:

<?xml version="1.0"?>
        <listversion>1.1 Final</listversion>
    <game name="My - Game (Japan) (Eu,Br)" index="true" image="0">
        <description>My - Game (Japan) (Eu,Br)</description>
        <rating>ESRB - T (Teen)</rating>
    <game name="Some Other Cool Game (Stuff) (Foo) (Bar)" index="" image="">
        <description>Some Other Cool Game (Stuff) (Foo) (Bar)</description>
        <rating>ESRB - T (Teen)</rating>

I need to remove everything after the first "(" in the "game name" field and replace this content with "(USA)", so the first game name needs to change from:

My - Game (Japan) (Eu,Br)
Some Other Cool Game (Stuff) (Foo) (Bar)


My - Game (USA)
Some Other Cool Game (USA)

I'm pretty close, and I'm able to get Powershell to print out the corrected names with:

[xml]$gameNames = Get-Content '.\myXML.xml' $ | foreach { $_.Name -replace '(.*', "(USA)" }

What I can't figure out is how to save hese new names in the "game name" nodes. I tried adding | Set-Content '.\myXML.xml' to the end of my pipe, but this just overwrites the original .xml with the renamed game names and none of the original data.

*Edit: There is a "\" before the "(" in the last -replace line. It doesn't show up, but (without spaces), the line should look like: -replace ' \ ( . * '

share|improve this question
up vote 1 down vote accepted
$ | foreach { $_.Name = $_.Name -replace '\(.*', "(USA)" }
share|improve this answer
Why has this been voted to be deleted? It works. – Martin Oct 14 '13 at 13:45
I have no idea. I have not yet been able to verify that this works but it looks correct! I will update you later, thanks! – root Oct 14 '13 at 14:30
That was it! The final step here was to correctly save my changes. Here's what my final version was: [xml]$gameNames = Get-Content '.\File.xml' $ | foreach { $_.Name = $_.Name -replace '\(.*', "(USA)" } $gameNames.Save("C:\File.xml") – root Oct 14 '13 at 23:21

You must log in to answer this question.

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