title: PowerShell / XML Get <Amount> value according to <Type>
body: <p>I'm not sure if the Title is correct for the question I need answered but here it goes.</p>
  <p>I need to grab the <code>&lt;Amount&gt;</code> value nested under a node where the <code>&lt;Type&gt;</code> node contains a particular value.</p>
  <p>Specifically, the amount under type <code>&lt;BurialFund&gt;</code>.</p>
  <p>The script I'm using (below the XML example) only returns the first amount from the <code>&lt;Asset&gt;</code> node so I need a way to specify which node to pull from according to the <code>&lt;Type&gt;</code> value.</p>
  <p>I believe this is the line I need assistance with: <code>$burial = Select-Xml -XPath '//AssetParent/Assets/Asset/Amount'</code></p>
  <p>EXCERPT FROM MY XML:</p>
  <pre><code>&lt;AssetParent&gt;
          &lt;Assets&gt;
              &lt;Asset&gt;
                  &lt;Type&gt;CheckingAccounts&lt;/Type&gt;
                  &lt;Amount&gt;100&lt;/Amount&gt;
              &lt;/Asset&gt;
              &lt;Asset&gt;
                  &lt;Type&gt;SavingsAccounts&lt;/Type&gt;
                  &lt;Amount&gt;200&lt;/Amount&gt;
              &lt;/Asset&gt;
              &lt;Asset&gt;
                  &lt;Type&gt;BurialFund&lt;/Type&gt;
                  &lt;Amount&gt;5000&lt;/Amount&gt;
              &lt;/Asset&gt;
          &lt;/Assets&gt;
      &lt;/AssetParent&gt;
  </code></pre>
  <p>MY SCRIPT</p>
  <pre><code>$dir = 'C:\Users\username\Documents\XML\Burial\'
  $manifest = 'C:\Users\username\Documents\XML\Burial\' + (Get-Date -Format yyyyMMdd) + '.csv'
  
  Get-ChildItem -Path $dir -Filter *xml | ForEach-Object {
  
    # Retrieve values from specified nodes.
    $interviewDate = Select-Xml -XPath '//CurrentDate' -Path $_.FullName -ErrorAction SilentlyContinue
    $burial = Select-Xml -XPath '//AssetParent/Assets/Asset/Amount' -Path $_.FullName -ErrorAction SilentlyContinue
    $dob = Select-Xml -XPath '//DOB' -Path $_.FullName -ErrorAction SilentlyContinue
    $lastName = Select-Xml -XPath '//LastName' -Path $_.FullName -ErrorAction SilentlyContinue
    $firstName = Select-Xml -XPath '//FirstName' -Path $_.FullName -ErrorAction SilentlyContinue
  
    # If values were retrieved succesfully.
    if ($interviewDate -and $burial -and $dob -and $lastName -and $firstName) {
  
      # Create a custom PSObject and set the values to corresponding properties.
      # Out-String Trim used to eliminate System.Object[] error.
      New-Object PSObject -Property @{
        InterviewDate = ($interviewDate.Node.InnerText | Out-String).Trim()
        Burial = ($burial.Node.InnerText | Out-String).Trim()
        DOB = ($dob.Node.InnerText | Out-String).Trim()
        LastName = ($lastName.Node.InnerText | Out-String).Trim()
        FirstName = ($firstName.Node.InnerText | Out-String).Trim()
      }
    }
    
    # Clear values.
    Clear-Variable interviewDate, burial, dob, lastName, firstName
    
      # Set order of columns.
      # Export data to the CSV file.
    } | Select-Object InterviewDate, Burial, DOB, LastName, FirstName | Export-Csv -Path $manifest -NoTypeInformation -append
  </code></pre>