Microsoft, Powershell, Send Key(s)

Posted on 2007-08-07
Last Modified: 2013-11-05
How can I do scripting commands like "Send Key(s)" or "Activate Window" using PowerShell?
Does this require the use of the Net Framework?
What is the best way to implement this type of scripting?
Question by:wsfindlater
    LVL 18

    Accepted Solution

    You can do create the Wscritp.Shell com object like this

    $myshell = New-Object -com "Wscript.Shell"

    To see Methods and Properties you have access to
    $myshell | get-members
    AppActivate              Method
    CreateShortcut           Method
    Exec                     Method
    ExpandEnvironmentStrings Method
    LogEvent                 Method
    Popup                    Method
    RegDelete                Method
    RegRead                  Method
    RegWrite                 Method
    Run                      Method
    SendKeys                 Method
    Environment              ParameterizedProperty
    CurrentDirectory         Property
    SpecialFolders           Property

    So to activate Notepad and send text do
    PS> $myshell.AppActivate("notepad");$myshell.sendkeys("Hello")

    You have to do these as two command seperated by ";" because you would steal focus if you did this
    PS> $myshell.AppActivate("notepad")
    PS> $myshell.sendkeys("Hello")

    Author Comment


    Wow! Thank-you very much. Now I can do everything with PowerShell.

    Featured Post

    Why You Should Analyze Threat Actor TTPs

    After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

    Join & Write a Comment

    If you have done a reformat of your hard drive and proceeded to do a successful Windows XP installation, you may notice that a choice between two operating systems when you start up the machine. Here is how to get rid of this: Click Start Clic…
    For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
    Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
    The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

    728 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now