PowerShell parse xml and save changes

Are you missing a cast?

$xml = [xml] gc $file.FullName

As @empo says, you need to cast the output of gc $file.FullName to [xml] e.g. $xml = [xml](gc $file.FullName). And then after making the change but before looping to the next file, you need to save the file e.g. $xml.Save($file.FullName).

This works with the sample project you provided:

$file = gi .\test.csproj
$pattern = 'TextTemplatingFileGenerator'
$xml = [xml](gc $file)
$xml | Where {$_.Project.ItemGroup.None.Generator -eq $pattern} |
       Foreach {$_.Project.ItemGroup.None.Generator = ''}
$xml.Save($file.Fullname)

Tags:

Xml

Powershell