title: How can I use the unmanaged UI Automation API from PowerShell
body: <p>The UI Automation API for Windows is available from two DLLs.
  One is a managed DLL, which is <code>C:\Program Files\Reference Assemblies\Microsoft\Framework\v3.0\UIAutomationClient.dll</code>.
  The other is an unmanaged DLL, which is <code>C:\Windows\System32\UIAutomationCore.dll</code>.
  According to <a href="https://social.msdn.microsoft.com/Forums/windowsdesktop/en-US/f3bbec4d-f7c6-4e66-8d74-9d3db562a6c3/ui-automation-cannot-find-an-element-inspect-object-can?forum=windowsaccessibilityandautomation" rel="nofollow noreferrer">this post</a>, the unmanaged API is superior to the managed API in terms of the number of visible elements, so I would like to use the unmanaged API.</p>
  
  <p>I have tried three approaches, but all of them failed.
  Would you tell me the correct approach?</p>
  
  <h1>Approach #1: New-Object -ComObject</h1>
  
  <pre class="lang-powershell prettyprint-override"><code>$uia = New-Object -ComObject &lt;ProgID of CUIAutomation&gt;
  $root = $uia.GetRootElement()
  </code></pre>
  
  <p>Failed because <code>New-Object</code> requires ProgID but <a href="https://msdn.microsoft.com/en-us/library/windows/desktop/ff384838(v=vs.85).aspx" rel="nofollow noreferrer"><code>CUIAutomation</code></a> does not have ProgID.</p>
  
  <h1>Approach #2: Instantiation from CLSID</h1>
  
  <p>The CLSID of <code>CUIAutomation</code> is <code>ff48dba4-60ef-4201-aa87-54103eef594e</code>, then,</p>
  
  <pre class="lang-powershell prettyprint-override"><code>$type = [Type]::GetTypeFromCLSID("ff48dba4-60ef-4201-aa87-54103eef594e")
  $uia = [Activator]::CreateInstance($type)
  $root = $uia.GetRootElement()
  </code></pre>
  
  <p>but failed with the following error message.
  I still do not know why.</p>
  
  <pre class="lang-none prettyprint-override"><code>Method invocation failed because [System.__ComObject] does not contain a method named 'GetRootElement'.
  At line:1 char:1
  + $root = $uia.GetRootElement()
  + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
      + FullyQualifiedErrorId : MethodNotFound
  </code></pre>
  
  <h1>Approach #3: Add-Type</h1>
  
  <pre class="lang-powershell prettyprint-override"><code>Add-Type -Path "C:\Windows\System32\UIAutomationCore.dll"
  $uia = New-Object UIAutomationClient.CUIAutomation
  $root = $uia.GetRootElement()
  </code></pre>
  
  <p>Failed because <code>Add-Type</code> expects managed DLLs.</p>
  
  <p>Error message:</p>
  
  <pre class="lang-none prettyprint-override"><code>Add-Type : Could not load file or assembly 'file:///C:\Windows\System32\UIAutomationCore.dll' or one of its dependencies. The module was expected to contain an assembly manifest. At line:1 char:1
  + Add-Type -Path "C:\Windows\System32\UIAutomationCore.dll"
  + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      + CategoryInfo          : NotSpecified: (:) [Add-Type], BadImageFormatException
      + FullyQualifiedErrorId : System.BadImageFormatException,Microsoft.PowerShell.Commands.AddTypeCommand
  </code></pre>
  
  <h1>Edit (2018-06-12)</h1>
  
  <p>I tried another approach. (and failed)</p>
  
  <h2>Approach #4: Interop DLL</h2>
  
  <p>I do not really understand what exactly the Interop DLL is, but <a href="https://social.msdn.microsoft.com/Forums/en-US/d0ad8e71-a2a8-49af-9916-11005ce794ba/creating-some-powershell-cmdlets-for-managing-windows-updates-via-wuapilib#8041782c-9c37-4f3e-a7e7-819a41379809" rel="nofollow noreferrer">this post</a> says the Interop DLL helped OP anyway.
  I installed Visual Studio and generated <code>Interop.UIAutomationClient.dll</code> by following the procedures of the post.</p>
  
  <pre class="lang-powershell prettyprint-override"><code>Add-Type -Path "Interop.UIAutomationClient.dll"
  $uia = New-Object UIAutomationClient.CUIAutomationClass
  $root = $uia.GetRootElement()
  $children = $root.FindAll([UIAutomationClient.TreeScope]::TreeScope_Children, $uia.CreateTrueCondition())
  </code></pre>
  
  <p>I succeeded in obtaining <code>$root</code>, but failed at the line of <code>$children</code> with the following error message.</p>
  
  <pre class="lang-none prettyprint-override"><code>Method invocation failed because [System.__ComObject] does not contain a method named 'FindAll'.
  At line:1 char:1
  + $children = $root.FindAll([UIAutomationClient.TreeScope]::TreeScope_C ...
  + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
      + FullyQualifiedErrorId : MethodNotFound
  </code></pre>
  
  <p>I still do not know why.</p>