GUI Automation in PowerShell using AutoIt

GUI Automation in PowerShell using AutoIt

AutoIt is an automation program I used to use a long time ago to do GUI automation and always found the programming model to make sense. The whole package seemed to be generally reliable and I'm not sure why it fell out of favour. One thing that didn't exist at that time was PowerShell so it's no surprise that I never thought to check for updates to it and if it now had a binding for PowerShell.

Part of the download from AutoIt now is a folder called AutoItX which you can use to do automation from PowerShell. It doesn't need an install so you can copy the folder to a machine where you need it and just import the .psd1 file.

$here = Split-Path -Path $MyInvocation.MyCommand.Path

Get-Module AutoItX | Remove-Module -Force
Import-Module $here\AutoItX.psd1 -Force


Invoke-AU3Run -Program notepad.exe

$NotepadTitle = "Untitled - Notepad"
Wait-AU3Win -Title $NotepadTitle

$hWnd = Get-AU3WinHandle -Title $notepadTitle

Show-AU3WinActivate -WinHandle $hWnd
$edit = Get-AU3ControlHandle -WinHandle $hWnd -Control "Edit1"

Set-AU3ControlText -ControlHandle $edit -WinHandle $hWnd -NewText "Hello World!" 

Sleep -Seconds 10

# Close even if prompted.

The core AutoIt assemblies are loaded via the PowerShell cmdlet assembly AutoItX3.PowerShell.dll. I'm not a huge fan of the naming used for the cmdlets, they seem very clunky to me but probably as a result of trying to avoid namespace clashes with other modules and PowerShell's verb conventions.