How to Provide Input to Applications with PowerShell

Something is useful automate an application task/setting from command line. From powershell  you can use .NET framework and using SendKeys to provide input to graphical applications.

Here a small example that open Notepad, type some text and open “Save as..” window:

# Load Assebly System.Windows.Forms  
[void] [System.Reflection.Assembly]::LoadWithPartialName(“‘System.Windows.Forms”)

# Load NotePad
& “$env:WINDIR\notepad.exe”

# Wait the application start for 1 sec 
Start-Sleep -m 1000

# Send keys
[System.Windows.Forms.SendKeys]::SendWait(“I'm wrinting something”)
[System.Windows.Forms.SendKeys]::SendWait(“{ENTER}”)
[System.Windows.Forms.SendKeys]::SendWait(“Powered by PowerShell”)

# Open "Save As.." menu (Alt+f+s)
[System.Windows.Forms.SendKeys]::SendWait(“%fs”)

# Wait menu opening for 0.5 sec 
Start-Sleep -m 500

Via: http://blogs.technet.com/b/heyscriptingguy/archive/2011/01/10/provide-input-to-applications-with-powershell.aspx

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s