Table of Contents

UI Automation (PS)

Sample (Calculator)

Import-Module "C:\Users\User\UIAutomation\UIAutomation.dll"
$process = Start-Process calc -PassThru
$window = Get-UiaWindow -Name 'Calculator'
for ( $i = 0; $i -lt 3; $i++ ) {
  $window | Get-UiaButton -Name 'Two' | Invoke-UiaButtonClick | Out-Null
  $window | Get-UiaButton -Name 'Plus' | Invoke-UiaButtonClick | Out-Null
  $window | Get-UiaButton -Name 'Five' | Invoke-UiaButtonClick | Out-Null
  $window | Get-UiaButton -Name 'Equals' | Invoke-UiaButtonClick | Out-Null
}

Sample (Notepad)

Import-Module "C:\Users\User\UIAutomation\UIAutomation.dll"
$datapath = "C:\Users\User\Desktop"
$filename = "sample.txt"
$exefname = "notepad"

## open file
$process = Start-Process $exefname -PassThru | Get-UiaWindow
$process.Keyboard.KeyPress([WindowsInput.Native.VirtualKeyCode]::MENU) | Out-Null
$process.Keyboard.KeyPress([WindowsInput.Native.VirtualKeyCode]::VK_F) | Out-Null
$process.Keyboard.KeyPress([WindowsInput.Native.VirtualKeyCode]::VK_O) | Out-Null

## select file
$openwnd = Get-UiaWindow -Name 'Open'
$openwnd.Keyboard.KeyPress([WindowsInput.Native.VirtualKeyCode]::F4) | Out-Null
$openwnd.Keyboard.KeyDown([WindowsInput.Native.VirtualKeyCode]::CONTROL) | Out-Null
$openwnd.Keyboard.KeyPress([WindowsInput.Native.VirtualKeyCode]::VK_A) | Out-Null
$openwnd.Keyboard.KeyUp([WindowsInput.Native.VirtualKeyCode]::CONTROL) | Out-Null
$openwnd.Keyboard.KeyPress([WindowsInput.Native.VirtualKeyCode]::DELETE) | Out-Null
$openwnd.Keyboard.TypeText($datapath) | Out-Null
$openwnd.Keyboard.KeyPress([WindowsInput.Native.VirtualKeyCode]::RETURN) | Out-Null
$openwnd.Keyboard.KeyPress([WindowsInput.Native.VirtualKeyCode]::TAB) | Out-Null
$openwnd.Keyboard.KeyPress([WindowsInput.Native.VirtualKeyCode]::TAB) | Out-Null
$openwnd.Keyboard.KeyPress([WindowsInput.Native.VirtualKeyCode]::TAB) | Out-Null
$openwnd.Keyboard.KeyPress([WindowsInput.Native.VirtualKeyCode]::TAB) | Out-Null
$openwnd.Keyboard.KeyPress([WindowsInput.Native.VirtualKeyCode]::TAB) | Out-Null
$openwnd.Keyboard.TypeText($filename) | Out-Null
$openwnd.Keyboard.KeyPress([WindowsInput.Native.VirtualKeyCode]::RETURN) | Out-Null
$openwnd.Keyboard.KeyPress([WindowsInput.Native.VirtualKeyCode]::RETURN) | Out-Null

## edit
$process.Keyboard.KeyPress([WindowsInput.Native.VirtualKeyCode]::VK_2) | Out-Null
$process.Keyboard.KeyPress([WindowsInput.Native.VirtualKeyCode]::ADD) | Out-Null
$process.Keyboard.KeyPress([WindowsInput.Native.VirtualKeyCode]::VK_5) | Out-Null
$process.Keyboard.KeyPress([WindowsInput.Native.VirtualKeyCode]::RETURN) | Out-Null

## save
$process.Keyboard.KeyPress([WindowsInput.Native.VirtualKeyCode]::MENU) | Out-Null
$process.Keyboard.KeyPress([WindowsInput.Native.VirtualKeyCode]::VK_F) | Out-Null
$process.Keyboard.KeyPress([WindowsInput.Native.VirtualKeyCode]::VK_S) | Out-Null
$process.Keyboard.KeyPress([WindowsInput.Native.VirtualKeyCode]::MENU) | Out-Null
$process.Keyboard.KeyPress([WindowsInput.Native.VirtualKeyCode]::VK_F) | Out-Null
$process.Keyboard.KeyPress([WindowsInput.Native.VirtualKeyCode]::VK_X) | Out-Null

Sample (Notepad and Screenshot)

Import-Module "C:\Users\User\UIAutomation\UIAutomation.dll"
$datapath = "C:\Users\User\Desktop\Sample"
$exefname = "notepad"

$array = dir $datapath
foreach ( $file in $array ) {
  $filefull = $file.FullName
  $filepath = $file.DirectoryName
  $fileprefix = $file.BaseName
  $fileext = $file.Extension
  $filename = "$fileprefix$fileext"
  $outfile = "$fileprefix.png"

  ## open file
  $process = Start-Process $exefname -PassThru -windowstyle Maximized | Get-UiaWindow
  $process.Keyboard.KeyPress([WindowsInput.Native.VirtualKeyCode]::MENU) | Out-Null
  $process.Keyboard.KeyPress([WindowsInput.Native.VirtualKeyCode]::VK_F) | Out-Null
  $process.Keyboard.KeyPress([WindowsInput.Native.VirtualKeyCode]::VK_O) | Out-Null

  ## select file
  $openwnd = Get-UiaWindow -Name 'Open'
  $openwnd.Keyboard.KeyPress([WindowsInput.Native.VirtualKeyCode]::F4) | Out-Null
  $openwnd.Keyboard.KeyDown([WindowsInput.Native.VirtualKeyCode]::CONTROL) | Out-Null
  $openwnd.Keyboard.KeyPress([WindowsInput.Native.VirtualKeyCode]::VK_A) | Out-Null
  $openwnd.Keyboard.KeyUp([WindowsInput.Native.VirtualKeyCode]::CONTROL) | Out-Null
  $openwnd.Keyboard.KeyPress([WindowsInput.Native.VirtualKeyCode]::DELETE) | Out-Null
  $openwnd.Keyboard.TypeText($filepath) | Out-Null
  $openwnd.Keyboard.KeyPress([WindowsInput.Native.VirtualKeyCode]::RETURN) | Out-Null
  $openwnd.Keyboard.KeyPress([WindowsInput.Native.VirtualKeyCode]::TAB) | Out-Null
  $openwnd.Keyboard.KeyPress([WindowsInput.Native.VirtualKeyCode]::TAB) | Out-Null
  $openwnd.Keyboard.KeyPress([WindowsInput.Native.VirtualKeyCode]::TAB) | Out-Null
  $openwnd.Keyboard.KeyPress([WindowsInput.Native.VirtualKeyCode]::TAB) | Out-Null
  $openwnd.Keyboard.KeyPress([WindowsInput.Native.VirtualKeyCode]::TAB) | Out-Null
  $openwnd.Keyboard.TypeText($filename) | Out-Null
  $openwnd.Keyboard.KeyPress([WindowsInput.Native.VirtualKeyCode]::RETURN) | Out-Null
  $openwnd.Keyboard.KeyPress([WindowsInput.Native.VirtualKeyCode]::RETURN) | Out-Null

  Start-Sleep -m 1000

  $process.Keyboard.KeyDown([WindowsInput.Native.VirtualKeyCode]::MENU) | Out-Null
  $process.Keyboard.KeyPress([WindowsInput.Native.VirtualKeyCode]::SNAPSHOT) | Out-Null
  $process.Keyboard.KeyUp([WindowsInput.Native.VirtualKeyCode]::MENU) | Out-Null

  $process.Keyboard.KeyPress([WindowsInput.Native.VirtualKeyCode]::MENU) | Out-Null
  $process.Keyboard.KeyPress([WindowsInput.Native.VirtualKeyCode]::VK_F) | Out-Null
  $process.Keyboard.KeyPress([WindowsInput.Native.VirtualKeyCode]::VK_X) | Out-Null

  $paintWnd = Start-Process mspaint -PassThru -windowstyle Maximized | Get-UiaWindow
  $paintWnd.Keyboard.KeyDown([WindowsInput.Native.VirtualKeyCode]::CONTROL) | Out-Null
  $paintWnd.Keyboard.KeyPress([WindowsInput.Native.VirtualKeyCode]::VK_V) | Out-Null
  $paintWnd.Keyboard.KeyUp([WindowsInput.Native.VirtualKeyCode]::CONTROL) | Out-Null

  $paintWnd.Keyboard.KeyDown([WindowsInput.Native.VirtualKeyCode]::CONTROL) | Out-Null
  $paintWnd.Keyboard.KeyPress([WindowsInput.Native.VirtualKeyCode]::VK_S) | Out-Null
  $paintWnd.Keyboard.KeyUp([WindowsInput.Native.VirtualKeyCode]::CONTROL) | Out-Null

  $openWnd = Get-UiaWindow -Name 'Save As'
  $openWnd.Keyboard.KeyPress([WindowsInput.Native.VirtualKeyCode]::F4) | Out-Null
  $openWnd.Keyboard.KeyDown([WindowsInput.Native.VirtualKeyCode]::CONTROL) | Out-Null
  $openWnd.Keyboard.KeyPress([WindowsInput.Native.VirtualKeyCode]::VK_A) | Out-Null
  $openWnd.Keyboard.KeyUp([WindowsInput.Native.VirtualKeyCode]::CONTROL) | Out-Null
  $openWnd.Keyboard.KeyPress([WindowsInput.Native.VirtualKeyCode]::DELETE) | Out-Null
  $openWnd.Keyboard.TypeText($filepath) | Out-Null
  $openWnd.Keyboard.KeyPress([WindowsInput.Native.VirtualKeyCode]::RETURN) | Out-Null

  $saveProcess = $openWnd | Get-UiaEdit -Name 'File name:'
  $saveProcess.Value = $outfile
  $saveProcess.Keyboard.KeyPress([WindowsInput.Native.VirtualKeyCode]::RETURN) | Out-Null

  $paintWnd.Keyboard.KeyPress([WindowsInput.Native.VirtualKeyCode]::MENU) | Out-Null
  $paintWnd.Keyboard.KeyPress([WindowsInput.Native.VirtualKeyCode]::VK_F) | Out-Null
  $paintWnd.Keyboard.KeyPress([WindowsInput.Native.VirtualKeyCode]::VK_X) | Out-Null

  Start-Sleep -m 1000
}

References

UI Automation PowerShell Extensions
http://uiautomation.codeplex.com/