14 responses to “Window GUI Automation from PowerShell”

  1. George Davis

    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:

    RUN,c:\temp\setup.exe
    SLEEP,15
    WINDOW,msiexec
    KEYS,{ENTER}
    SLEEP,2
    KEYS,{UP}
    KEYS,{ENTER}
    SLEEP,2
    KEYS,{ENTER}
    SLEEP,2
    KEYS,{ENTER}
    SLEEP,2
    KEYS,{ENTER}
    SLEEP,2
    KEYS,{TAB}
    KEYS,{TAB}
    KEYS,{TAB}
    KEYS,mySqlPassword
    KEYS,{TAB}
    KEYS,{END}
    KEYS,\SQL2005
    KEYS,{ENTER}
    SLEEP,2
    KEYS,{ENTER}
    SLEEP,2
    KEYS,{ENTER}
    SLEEP,2
    KEYS,{ENTER}
    SLEEP,2
    KEYS,{ENTER}
    SLEEP,2
    KEYS,{ENTER}
    SLEEP,2
    WAITFORBUTTON,&Finish,10,{ENTER}

    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!
    George

  2. Evan

    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. rama

    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

  4. rama

    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

  5. rama

    hi,

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

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

  6. seaJhawk

    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?

    Thanks,
    seaJhawk

  7. Jaykul

    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…

  8. Jaykul

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

  9. suxiaojack

    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?