title: How do I edit XML using Powershell?
body: <p>I'm trying to edit some xml in Powershell. Here's what I'm doing:</p>
  
  <pre><code>$path = 'C:\Projects\NuGetTestPackage\NuGetTestPackage'
  $projName = 'NuGetTestPackage1.csproj'
  cd $path
  $file = gi $projName
  $pattern = 'TextTemplatingFileGenerator'
  $xml = [xml](gc $file)
  $node = [xml] $xml.Project.ItemGroup.None.Generator | 
      Where { $_.Project.ItemGroup.None.Generator -eq $pattern } |
      Foreach {$_.Project.ItemGroup.None.Generator = ''}
  $xml.Save($file.Fullname)
  </code></pre>
  
  <p>Here's the XML:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"&gt;
    &lt;PropertyGroup&gt;
      &lt;Configuration Condition=" '$(Configuration)' == '' "&gt;Debug&lt;/Configuration&gt;
      &lt;Platform Condition=" '$(Platform)' == '' "&gt;AnyCPU&lt;/Platform&gt;
      &lt;ProductVersion&gt;
      &lt;/ProductVersion&gt;
      &lt;SchemaVersion&gt;2.0&lt;/SchemaVersion&gt;
      &lt;ProjectGuid&gt;{E750ACF1-17B5-4D83-8FC8-5A4488193099}&lt;/ProjectGuid&gt;
      &lt;ProjectTypeGuids&gt;{E53F8FEA-EAE0-44A6-8774-FFD645390401};{349c5851-65df-11da-9384-00065b846f21};{fae04ec0-301f-11d3-bf4b-00c04f79efbc}&lt;/ProjectTypeGuids&gt;
      &lt;OutputType&gt;Library&lt;/OutputType&gt;
      &lt;AppDesignerFolder&gt;Properties&lt;/AppDesignerFolder&gt;
      &lt;RootNamespace&gt;NuGetTestPackage&lt;/RootNamespace&gt;
      &lt;AssemblyName&gt;NuGetTestPackage&lt;/AssemblyName&gt;
      &lt;TargetFrameworkVersion&gt;v4.0&lt;/TargetFrameworkVersion&gt;
      &lt;MvcBuildViews&gt;false&lt;/MvcBuildViews&gt;
      &lt;UseIISExpress&gt;false&lt;/UseIISExpress&gt;
    &lt;/PropertyGroup&gt;
    &lt;PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' "&gt;
      &lt;DebugSymbols&gt;true&lt;/DebugSymbols&gt;
      &lt;DebugType&gt;full&lt;/DebugType&gt;
      &lt;Optimize&gt;false&lt;/Optimize&gt;
      &lt;OutputPath&gt;bin\&lt;/OutputPath&gt;
      &lt;DefineConstants&gt;DEBUG;TRACE&lt;/DefineConstants&gt;
      &lt;ErrorReport&gt;prompt&lt;/ErrorReport&gt;
      &lt;WarningLevel&gt;4&lt;/WarningLevel&gt;
    &lt;/PropertyGroup&gt;
    &lt;PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' "&gt;
      &lt;DebugType&gt;pdbonly&lt;/DebugType&gt;
      &lt;Optimize&gt;true&lt;/Optimize&gt;
      &lt;OutputPath&gt;bin\&lt;/OutputPath&gt;
      &lt;DefineConstants&gt;TRACE&lt;/DefineConstants&gt;
      &lt;ErrorReport&gt;prompt&lt;/ErrorReport&gt;
      &lt;WarningLevel&gt;4&lt;/WarningLevel&gt;
    &lt;/PropertyGroup&gt;
    &lt;ItemGroup&gt;
      &lt;Reference Include="EntityFramework, Version=4.1.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089, processorArchitecture=MSIL"&gt;
        &lt;Private&gt;True&lt;/Private&gt;
        &lt;HintPath&gt;..\packages\EntityFramework.4.1.10331.0\lib\EntityFramework.dll&lt;/HintPath&gt;
      &lt;/Reference&gt;
      &lt;Reference Include="System.Data.Entity" /&gt;
      &lt;Reference Include="System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL" /&gt;
      &lt;Reference Include="System.Web.WebPages, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL" /&gt;
      &lt;Reference Include="System.Web.Helpers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL" /&gt;
      &lt;Reference Include="Microsoft.CSharp" /&gt;
      &lt;Reference Include="System" /&gt;
      &lt;Reference Include="System.Data" /&gt;
      &lt;Reference Include="System.Drawing" /&gt;
      &lt;Reference Include="System.Web.DynamicData" /&gt;
      &lt;Reference Include="System.Web.Entity" /&gt;
      &lt;Reference Include="System.Web.ApplicationServices" /&gt;
      &lt;Reference Include="System.ComponentModel.DataAnnotations" /&gt;
      &lt;Reference Include="System.Core" /&gt;
      &lt;Reference Include="System.Data.DataSetExtensions" /&gt;
      &lt;Reference Include="System.Xml.Linq" /&gt;
      &lt;Reference Include="System.Web" /&gt;
      &lt;Reference Include="System.Web.Extensions" /&gt;
      &lt;Reference Include="System.Web.Abstractions" /&gt;
      &lt;Reference Include="System.Web.Routing" /&gt;
      &lt;Reference Include="System.Xml" /&gt;
      &lt;Reference Include="System.Configuration" /&gt;
      &lt;Reference Include="System.Web.Services" /&gt;
      &lt;Reference Include="System.EnterpriseServices" /&gt;
    &lt;/ItemGroup&gt;
    &lt;ItemGroup&gt;
      &lt;Compile Include="Application\ApplicationConfigurationSettings.cs" /&gt;
      &lt;Compile Include="Global.asax.cs"&gt;
        &lt;DependentUpon&gt;Global.asax&lt;/DependentUpon&gt;
      &lt;/Compile&gt;
      &lt;Compile Include="Properties\AssemblyInfo.cs" /&gt;
    &lt;/ItemGroup&gt;
    &lt;ItemGroup&gt;
      &lt;Content Include="Content\themes\base\images\ui-bg_flat_0_aaaaaa_40x100.png" /&gt;
      &lt;Content Include="Content\themes\base\images\ui-bg_flat_75_ffffff_40x100.png" /&gt;
      &lt;Content Include="Content\themes\base\images\ui-bg_glass_55_fbf9ee_1x400.png" /&gt;
      &lt;Content Include="Content\themes\base\images\ui-bg_glass_65_ffffff_1x400.png" /&gt;
      &lt;Content Include="Content\themes\base\images\ui-bg_glass_75_dadada_1x400.png" /&gt;
      &lt;Content Include="Content\themes\base\images\ui-bg_glass_75_e6e6e6_1x400.png" /&gt;
      &lt;Content Include="Content\themes\base\images\ui-bg_glass_95_fef1ec_1x400.png" /&gt;
      &lt;Content Include="Content\themes\base\images\ui-bg_highlight-soft_75_cccccc_1x100.png" /&gt;
      &lt;Content Include="Content\themes\base\images\ui-icons_222222_256x240.png" /&gt;
      &lt;Content Include="Content\themes\base\images\ui-icons_2e83ff_256x240.png" /&gt;
      &lt;Content Include="Content\themes\base\images\ui-icons_454545_256x240.png" /&gt;
      &lt;Content Include="Content\themes\base\images\ui-icons_888888_256x240.png" /&gt;
      &lt;Content Include="Content\themes\base\images\ui-icons_cd0a0a_256x240.png" /&gt;
      &lt;Content Include="Content\themes\base\jquery.ui.accordion.css" /&gt;
      &lt;Content Include="Content\themes\base\jquery.ui.all.css" /&gt;
      &lt;Content Include="Content\themes\base\jquery.ui.autocomplete.css" /&gt;
      &lt;Content Include="Content\themes\base\jquery.ui.base.css" /&gt;
      &lt;Content Include="Content\themes\base\jquery.ui.button.css" /&gt;
      &lt;Content Include="Content\themes\base\jquery.ui.core.css" /&gt;
      &lt;Content Include="Content\themes\base\jquery.ui.datepicker.css" /&gt;
      &lt;Content Include="Content\themes\base\jquery.ui.dialog.css" /&gt;
      &lt;Content Include="Content\themes\base\jquery.ui.progressbar.css" /&gt;
      &lt;Content Include="Content\themes\base\jquery.ui.resizable.css" /&gt;
      &lt;Content Include="Content\themes\base\jquery.ui.selectable.css" /&gt;
      &lt;Content Include="Content\themes\base\jquery.ui.slider.css" /&gt;
      &lt;Content Include="Content\themes\base\jquery.ui.tabs.css" /&gt;
      &lt;Content Include="Content\themes\base\jquery.ui.theme.css" /&gt;
      &lt;Content Include="Global.asax" /&gt;
      &lt;Content Include="Content\Site.css" /&gt;
      &lt;Content Include="Scripts\jquery-1.5.1-vsdoc.js" /&gt;
      &lt;Content Include="Scripts\jquery-1.5.1.js" /&gt;
      &lt;Content Include="Scripts\jquery-1.5.1.min.js" /&gt;
      &lt;Content Include="Scripts\jquery-ui-1.8.11.js" /&gt;
      &lt;Content Include="Scripts\jquery-ui-1.8.11.min.js" /&gt;
      &lt;Content Include="Scripts\jquery.validate-vsdoc.js" /&gt;
      &lt;Content Include="Scripts\jquery.validate.js" /&gt;
      &lt;Content Include="Scripts\jquery.validate.min.js" /&gt;
      &lt;Content Include="Scripts\modernizr-1.7.js" /&gt;
      &lt;Content Include="Scripts\modernizr-1.7.min.js" /&gt;
      &lt;Content Include="T4\WebConfigSettingGeneratorScript.txt"&gt;
        &lt;AutoGen&gt;True&lt;/AutoGen&gt;
        &lt;DesignTime&gt;True&lt;/DesignTime&gt;
        &lt;DependentUpon&gt;WebConfigSettingGeneratorScript.tt&lt;/DependentUpon&gt;
      &lt;/Content&gt;
      &lt;Content Include="T4\WebConfigSettingGeneratorScript1.txt" /&gt;
      &lt;Content Include="Web.config" /&gt;
      &lt;Content Include="Web.Debug.config"&gt;
        &lt;DependentUpon&gt;Web.config&lt;/DependentUpon&gt;
      &lt;/Content&gt;
      &lt;Content Include="Web.Release.config"&gt;
        &lt;DependentUpon&gt;Web.config&lt;/DependentUpon&gt;
      &lt;/Content&gt;
      &lt;Content Include="Scripts\jquery.unobtrusive-ajax.js" /&gt;
      &lt;Content Include="Scripts\jquery.unobtrusive-ajax.min.js" /&gt;
      &lt;Content Include="Scripts\jquery.validate.unobtrusive.js" /&gt;
      &lt;Content Include="Scripts\jquery.validate.unobtrusive.min.js" /&gt;
      &lt;Content Include="Scripts\MicrosoftAjax.js" /&gt;
      &lt;Content Include="Scripts\MicrosoftAjax.debug.js" /&gt;
      &lt;Content Include="Scripts\MicrosoftMvcAjax.js" /&gt;
      &lt;Content Include="Scripts\MicrosoftMvcAjax.debug.js" /&gt;
      &lt;Content Include="Scripts\MicrosoftMvcValidation.js" /&gt;
      &lt;Content Include="Scripts\MicrosoftMvcValidation.debug.js" /&gt;
      &lt;Content Include="Views\Web.config" /&gt;
      &lt;Content Include="Views\_ViewStart.cshtml" /&gt;
      &lt;Content Include="Views\Shared\Error.cshtml" /&gt;
      &lt;Content Include="Views\Shared\_Layout.cshtml" /&gt;
    &lt;/ItemGroup&gt;
    &lt;ItemGroup&gt;
      &lt;Folder Include="Controllers\" /&gt;
      &lt;Folder Include="Models\" /&gt;
    &lt;/ItemGroup&gt;
    &lt;ItemGroup&gt;
      &lt;Content Include="packages.config" /&gt;
    &lt;/ItemGroup&gt;
    &lt;ItemGroup&gt;
      &lt;None Include="CodeTemplates\AddController\Controller.tt" /&gt;
      &lt;None Include="CodeTemplates\AddController\ControllerWithContext.tt" /&gt;
      &lt;None Include="CodeTemplates\AddView\CSHTML\Create.tt" /&gt;
      &lt;None Include="CodeTemplates\AddView\CSHTML\Delete.tt" /&gt;
      &lt;None Include="CodeTemplates\AddView\CSHTML\Details.tt" /&gt;
      &lt;None Include="CodeTemplates\AddView\CSHTML\Edit.tt" /&gt;
      &lt;None Include="CodeTemplates\AddView\CSHTML\Empty.tt" /&gt;
      &lt;None Include="CodeTemplates\AddView\CSHTML\List.tt" /&gt;
      &lt;None Include="T4\Generators\WebConfigGenerator.tt" /&gt;
      &lt;None Include="T4\Templates\WebConfigTemplate.tt" /&gt;
      &lt;None Include="T4\WebConfigSettingGeneratorScript.tt"&gt;
        &lt;Generator&gt;TextTemplatingFileGenerator&lt;/Generator&gt;
        &lt;LastGenOutput&gt;WebConfigSettingGeneratorScript.txt&lt;/LastGenOutput&gt;
      &lt;/None&gt;
      &lt;None Include="T4\WebConfigSettingGeneratorScript.tt.log"&gt;
        &lt;DependentUpon&gt;WebConfigSettingGeneratorScript.tt&lt;/DependentUpon&gt;
      &lt;/None&gt;
    &lt;/ItemGroup&gt;
    &lt;ItemGroup&gt;
      &lt;Service Include="{508349B6-6B84-4DF5-91F0-309BEEBAD82D}" /&gt;
    &lt;/ItemGroup&gt;
    &lt;Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" /&gt;
    &lt;Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v10.0\WebApplications\Microsoft.WebApplication.targets" /&gt;
    &lt;!-- To modify your build process, add your task inside one of the targets below and uncomment it. 
     Other similar extension points exist, see Microsoft.Common.targets.
    &lt;Target Name="BeforeBuild"&gt;
    &lt;/Target&gt;
    &lt;Target Name="AfterBuild"&gt;
    &lt;/Target&gt; --&gt;
    &lt;Target Name="MvcBuildViews" AfterTargets="AfterBuild" Condition="'$(MvcBuildViews)'=='true'"&gt;
      &lt;AspNetCompiler VirtualPath="temp" PhysicalPath="$(WebProjectOutputDir)" /&gt;
    &lt;/Target&gt;
    &lt;ProjectExtensions&gt;
      &lt;VisualStudio&gt;
        &lt;FlavorProperties GUID="{349c5851-65df-11da-9384-00065b846f21}"&gt;
          &lt;WebProjectProperties&gt;
            &lt;UseIIS&gt;False&lt;/UseIIS&gt;
            &lt;AutoAssignPort&gt;True&lt;/AutoAssignPort&gt;
            &lt;DevelopmentServerPort&gt;55059&lt;/DevelopmentServerPort&gt;
            &lt;DevelopmentServerVPath&gt;/&lt;/DevelopmentServerVPath&gt;
            &lt;IISUrl&gt;
            &lt;/IISUrl&gt;
            &lt;NTLMAuthentication&gt;False&lt;/NTLMAuthentication&gt;
            &lt;UseCustomServer&gt;False&lt;/UseCustomServer&gt;
            &lt;CustomServerUrl&gt;
            &lt;/CustomServerUrl&gt;
            &lt;SaveServerSettingsInUserFile&gt;False&lt;/SaveServerSettingsInUserFile&gt;
          &lt;/WebProjectProperties&gt;
        &lt;/FlavorProperties&gt;
      &lt;/VisualStudio&gt;
    &lt;/ProjectExtensions&gt;
  &lt;/Project&gt;
  </code></pre>
  
  <p>The idea is to clear the value of the Generator node. I can't seem to get this code to work. Am I accessing the xml properties incorrectly? It seems like I get a value back in the for each, but I'm having trouble accessing it correctly.</p>
  
  <hr>
  
  <p>Ok, thanks @Keith Hill. Here's the answer:</p>
  
  <pre><code>$path = 'C:\Projects\NuGetTestPackage\NuGetTestPackage'
  $ns = @{msb = 'http://schemas.microsoft.com/developer/msbuild/2003'}
  cd $path
  $results = 0
  $projName = 'NuGetTestPackage1.csproj'
  $file = gi $projName
  $xml = [xml](gc $projName)
  $xml | Select-Xml "//msb:Generator" -Namespace $ns | 
         Foreach { 
              $_.Node.set_InnerText('')
              $results = 1
         }
  if($results -eq 1){
      $xml.Save($file.FullName)
  }
  </code></pre>