title: xml node exists using variables in powershell
body: <p>I am trying to determine if a Mailbox with a certain DisplayName exists in my powershell script (see example xml below). I can find the node if I hard-code the value, but I have not been able to pass this value as a parameter. I have tried various ways of escaping, using single quotes and double, but none of it is going the right way.</p>
  
  <p>The ultimate goal is to create the Mailbox node if one doesn't exist with the same DisplayName. There may be 0 or more Mailbox nodes.</p>
  
  <p>Here's a sample xml:</p>
  
  <pre><code>&lt;Profiler&gt;
    &lt;Mailboxes&gt;
      &lt;Mailbox&gt;
        &lt;DisplayName&gt;django&lt;/DisplayName&gt;
      &lt;/Mailbox&gt;
    &lt;/Mailboxes&gt;
  &lt;/Profiler&gt;
  </code></pre>
  
  <p>This is the piece of code, I want executed if the node doesn't exist:</p>
  
  <pre><code>$newMailbox = $xmlData.CreateElement("Mailbox")
  $newDisplayName = $xmlData.CreateElement("DisplayName")
  $newDisplayNameText = $xmlData.CreateTextNode($mailboxName)
  $newDisplayName.AppendChild($newDisplayNameText)
  $newMailbox.AppendChild($newDisplayName)
  $mailboxes = $xmlData.SelectSingleNode("./Profiler/Mailboxes")
  $mailboxes.AppendChild($newMailbox)
  </code></pre>
  
  <p>This piece of code works:</p>
  
  <pre><code>$users = $xmlData.SelectNodes("./Profiler/Mailboxes/Mailbox/DisplayName[text() = 'django']")
  write-host $users.count
  </code></pre>
  
  <blockquote>
    <p>output from script: 1</p>
  </blockquote>
  
  <p>This doesn't:</p>
  
  <pre><code>$mailboxName = "django"
  $users = $xmlData.SelectNodes("./Profiler/Mailboxes/Mailbox/DisplayName[text() = $mailboxName]")
  write-host $users.count 
  </code></pre>
  
  <blockquote>
    <p>output from script: 0</p>
  </blockquote>
  
  <p>I also tried this:</p>
  
  <pre><code>$mailboxName = "django"
  $users= $xmlData.SelectNodes("./Profiler/Mailboxes/Mailbox[DisplayName = $mailbox]")
  </code></pre>
  
  <blockquote>
    <p>output from script: Exception calling "SelectNodes" with "1" argument(s): "Expression must evaluate to a node-set."</p>
  </blockquote>