title: Get first element's attribute in Powershell
body: <p>I am writing an automated site backup powershell script. </p>
  
  <p>I have the following xml in an [xml] casted variable obtained from appcmd.exe:</p>
  
  <pre><code>&lt;application path="/" applicationPool="komplus.ua-PROD"&gt;     
    &lt;virtualDirectoryDefaults /&gt;  
    &lt;virtualDirectory path="/" physicalPath="C:\WEB\PROD\komplus.ua" /&gt;       
  &lt;/application&gt;
  &lt;application path="/kb" applicationPool="komplus.ua-PROD" enabledProtocols="http, https"&gt;
    &lt;virtualDirectoryDefaults /&gt; 
    &lt;virtualDirectory path="/" physicalPath="C:\WEB\PROD\komplus.ua\kb" /&gt;
  &lt;/application&gt;
  </code></pre>
  
  <p>The xml is in the variable $xmlInput.</p>
  
  <p>I need to get the value of the physicalPath attribute of VirtualDirectory element in first application element. </p>
  
  <p>I've tried several options, including custom Get-XmlElement functions, but can't seem to get to the value.</p>
  
  <p>Currently I have:</p>
  
  <pre><code> $selectResult = $xmlInput.application.virtualDirectory | Select physicalPath
  </code></pre>
  
  <p>But the $selectResult is null. </p>
  
  <p>Please assist in which parsing is correct to get the value I need.</p>
  
  <p><strong>Update</strong></p>
  
  <p>Here is a full function I am using to get the full path:</p>
  
  <pre><code>function GetPhysicalPath ([String]$siteName) {
  
  
  function Get-PipelineInput
  {            
      end {
          $inputAsVar = 'Current input is:' + $input
          write-host "##teamcity[message text='$inputAsVar']"
  
          write-host "##teamcity[message text='About to cast to xml']"
          $xmlInput = [xml]@"&lt;root&gt;$input&lt;/root&gt;"@
  
          write-host "##teamcity[message text='Cast to xml finished.']"                
  
          $selectResult = $xml.root.Application | Where-Object { $_.path -eq '/' } | ForEach-Object { $_.virtualDirectory.physicalPath }
  
          write-host "##teamcity[message text='$selectResult']"
  
          return $selectResult
  
          #$node = Get-XmlNode -XmlDocument $xmlInput -NodePath "application.virtualDirectory"
  
          #$nodeAsVar = 'Single node is:' + $node
          #write-host "##teamcity[message text='$nodeAsVar']"
          #if ($node -eq $null) { return $null }
  
          #return $node.physicalPath
      }
  }
  
  $appCmd = "$Env:SystemRoot\system32\inetsrv\appcmd.exe"
  
  return &amp; $appCmd list app /site.name:"$siteName" /config | Get-PipelineInput
  }
  </code></pre>