<#
Send a sequence of keys to maximize an application window

.PARAMETER ApplicationTitle
The title of the application window

.PARAMETER WaitTime
An optional number of seconds to wait after the sending of the keys

.EXAMPLE
Maximize "Movies & TV"  -WaitTime 5 -VideoPlayMode 1 
#>

param (
    [Parameter(Mandatory=$True,Position=1)]
    [string]
    $ApplicationTitle,

    [Parameter(Mandatory=$false)]
    [int] $WaitTime,

    [Parameter(Mandatory=$false)]
    [int] $VideoPlayMode

    )

# load assembly cotaining class System.Windows.Forms.SendKeys
[void] [Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
#Add-Type -AssemblyName System.Windows.Forms

# add a C# class to access the WIN32 API SetForegroundWindow
Add-Type @"
    using System;
    using System.Runtime.InteropServices;
    public class StartActivateProgramClass {
        [DllImport("user32.dll")]
        [return: MarshalAs(UnmanagedType.Bool)]
        public static extern bool SetForegroundWindow(IntPtr hWnd);
    }
"@

# get the applications with the specified title
$p = Get-Process | Where-Object { $_.MainWindowTitle.ToLower().Contains($ApplicationTitle.ToLower()) -and !$_.MainWindowTitle.ToLower().Contains("command") -and !$_.MainWindowTitle.ToLower().Contains("powershell")}
if ($p) 
{
    # get the window handle of the first application
    $h = $p[0].MainWindowHandle
    # set the application to foreground
    [void] [StartActivateProgramClass]::SetForegroundWindow($h)

    # send the keys sequence
    # more info on MSDN at http://msdn.microsoft.com/en-us/library/System.Windows.Forms.SendKeys(v=vs.100).aspx
    [System.Windows.Forms.SendKeys]::SendWait("% ")
    Start-Sleep -Seconds 1
    [System.Windows.Forms.SendKeys]::SendWait("X ")
    Start-Sleep -Seconds 1
    if ($ApplicationTitle.Equals("Movies & TV"))
    {
        if ($VideoPlayMode -eq 1)
        {
            [System.Windows.Forms.SendKeys]::SendWait("{TAB}")
            [System.Windows.Forms.SendKeys]::SendWait("{TAB}")
            Start-Sleep -Seconds 2
            #[System.Windows.Forms.SendKeys]::SendWait(" ")
            [System.Windows.Forms.SendKeys]::SendWait("{ENTER}")
        }
    }
    if ($WaitTime) 
    {
        Start-Sleep -Seconds $WaitTime
    }

}