Window GUI Automation from PowerShell

Well, I am going to put this all on CodePlex tomorrow, but I promised a few people that I’d throw it up here for them yesterday, so I figured it’s past time to post it … without further ado. [new] The Window Automation Snapin and it’s source should be downloaded from the CodePlex project where updates can be tracked. :)

This is basically an upgrade to the Win32.Windows snapin I released a while back, the one thing that’s missing in this release that was possible in that one is using frame-set definitions to position windows. That will make it back in eventually, but in the meantime, I present some major new additions which add up to the ability to do 90% of what you’d want to do in testing or automating your winforms app’s UI.

  • Select-Window – pick windows by process name or window caption (with wildcard support)
  • Select-Control – pick controls by class and/or name and/or index (with wildcard support)
  • Send-Click – send mouse clicks (any button, with any modifier keys)
  • Send-Keys – Windows.Forms.SendKeys lets you send keys … try this: Select-Window notepad | Send-Keys "%(ea)Testing{Enter}{F5}" (and for extra fun, try it with multiple notepad windows open).
  • Set-WindowActive – yeah, just activates the window
  • Set-WindowPosition – set any one of (or all of) top, left, width, height on a window … or maximize/minimize/restore
  • Get-WindowPosition – get the position (kind-of redundant, actually, since the Window object has it’s position as a property)
  • Remove-Window – closes the specified window

There’s no help right now, so get used to using Get-Command -PSSnapin WindowAutomation and checking out the ParameterSets … hopefully you can figure it out from that for now. Oh, one other thing, there might be a few extras hiding in these:

  • [Huddled.Win32.WindowExtenders] | Get-Member -static
  • [Huddled.Win32.WindowFinder] | Get-Member -static

For now, that will have to do, it’s past time for bed. You might want to play with this and the out-voice script I posted on CodePlex awhile back, it makes things extra fun.

Similar Posts:

14 thoughts on “Window GUI Automation from PowerShell”

  1. Thanks for writing this, Joel! I’ve used it to automate the installation of our product on my development laptop, which I have to do very frequently. I’ve had a batch file for a year that would un-install the product but didn’t want to load our QA app just to automate the install. Now I’ve done it with your DLL and PowerShell in a very lightweight fashion! I wrote a PowerShell script to read an “instruction” file that looks like:


    The WINDOW instruction is only specified at the beginning of the file so that when the script reads the KEYS command, it knows which window to which to send the keystrokes. On most of the screens, I just hit enter but you can probably see one where I enter a SQL password.

    My script reads each line and executes the appropriate command using your DLL. I still have the RUN one to complete but everything else works. The WAITFORBUTTON instruction will cause my script to look for the Finish button and it not found, keep checking every 10 seconds and when found, hit Enter.

    Again, when I get the RUN instruction implemented, I’ll send you the PS1 file and a sample instruction file. My network admin guy drooled a lot when he saw what I had done and wants to automate all the boring installs/upgrades he has to do all the time.

    Many thanks again!

  2. I am having some problems running this code. Here are some of them I’ve run across so far:

    1)for some strange reason “select-window powershell“ doesn’t return anything even though “select-window notepad“ does

    for the following problems, “$window = select-window notepad

    2)Both “get-windowposition $window” and “$window | get-windowposition” will not recognize $window as a parameter

    This happens with remove-window, and send-keys

    3) “set-windowposition -width 2” does nothing and “set-windowposition $window -maximize “ gives and error

    I can send the code I am trying with the errors if necessary

  3. Hm, looks like I messed up the parameter sets somehow. I’ll have to push out a new release on CodePlex.

    Select-Window doesn’t seem to like selecting the console windows … and I don’t know why (that is to say, I’ll have to crack the code open, so gimme a few days). They show up if you do Select-Window | ? {$_.ProcessName -eq "powershell"}

    Get-WindowPosition and Remove-Window are broken because I messed up their parameter sets — they can’t resolve which parameter set to use, because they have two sets which are basically identical. That was a typo on my part. Luckily, you can use the Position attribute on the Window objects, and the CloseMainWindow() and CloseProcess() methods…

    Send-Keys seems to work fine here … either via the pipeline or as an argument:

    $np = Select-Window notepad
    Send-Keys $np "what?"
    Select-Window notepad | Send-Keys "%{f4}n"
  4. Hi,

    How can I install the windows automation snapin in my power shell environment. I am getting an error like

    File C:\Documents and settings\test\Desktop\windowautomation\WindowAutomation\Install.ps1 cannot be loaded. The file C:\Documents and Settings\test\Desktop\windowautomation\WindowAutomation\Install.ps1 is not digitally signed. The script will not execute on the system. Please see “get-help about_signing” for more details..
    At line:0 char:0

  5. You need to run “Get-Help about_signing” as it suggests. By default PowerShell requires everything to be cryptographically signed. My stuff isn’t, so you have to use the “Set-ExecutionPolicy” cmdlet to set your policy to “RemoteSigned”... and you might have to “unblock” the script (this is all described in the “Get-Help about_signing” document.

    If you’re using CTP2, you can use Add-Module instead of using the installer.

    Also, you should use the version on CodePlex because it’s been updated from the one I posted. I’ve changed that in the article.

  6. Hi,

    I am using CTP2 only can you please guide me how can I install

    bacaz Installutil is not recognised as a cmdlet. It will be helpful for me how to install the GUI Automation Snapin from the scrach.

    Do I need any other .exe to proceed the installation. The snapin which is not working out for me.

    thanks in advance

  7. hi,

    If I do Add-module I am getting this error.

    Add-Module .\WindowAutomation
    Add-Module : The specified module ‘.\WindowAutomation’ was not loaded because the module source file was not found in any package
    At line:1 char:11
    + Add-Module <<<< .\WindowAutomation

  8. For CTP2, you need to either put the dll into a specific folder … Documents\WindowsPowerShell\Packages\WindowsAutomation\WindowsAutomation.dll in which case you’ll be able to call just:

    Add-Module WindowsAutomation

    Otherwise, you can load it by specifying the path to the actual .dll:

    Add-Module .\WindowsAutomation.dll

  9. Hi Jaykul,
    My app that I'm trying to automate pops up a modal dialog when I click a key. However, I can't find any way to select that modal dialog – should this be possible?


  10. Yeah, that should be possible. If it’s modal, it should be the “active” window anyway, so you should be able to get it that way, or just send-keys and have them go to it by default…

  11. Yes it should! At the very least you should be able to select it as the “active” window

  12. Could you publish your project for javascript ?JS supported 'sendkey' very good yet.So,is here a way to 'CreateObject' for your dll in javascript?

    1. I can try setting the COM flag in the next build, but I think the right answer here is: if you’re trying to automate windows from javascript, you’re using the wrong tool.

  13. I can try setting the COM flag in the next build, but I think the right answer here is: if you’re trying to automate windows from javascript, you’re using the wrong tool.

Comments are closed.