Search code examples
powershellchmlaunching-application

PS: Launch an app and open chm


Many applications have associated help that you invoke by pressing F1.
What PowerShell command would launch the application AND open the help?
Applications I have in mind are Powershell ISE, Visual Studio, Microsoft Office, etc.
Is there a standard mechanism for this?

Thanks.


Solution

  • There's no one command to do it but you can do it by:

    1. Launching the application.
    2. Activating it's window
    3. Sending the F1 key to it.

    Here's how:

    Launch an application:

    Start-Process -FilePath C:\The\Path\To\Program.exe 
    

    Wait for it:

    $WindowTitle = "The App You're Waiting For"
    while ($true) {
        Start-Sleep -Seconds 1
        $p = get-process | ? {$_.MainWindowTitle -match $WindowTitle}
        if ($p) {break}
    }
    

    Activate the window:

    [void] [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.VisualBasic")
    [Microsoft.VisualBasic.Interaction]::AppActivate($WindowTitle)
    

    Send the F1 key:

    [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
    [System.Windows.Forms.SendKeys]::SendWait("{F1}")