Monthly Archives: August 2013

Two days ago, I used our test installation of System Center App Controller to create a new virtual machine in one of our Windows Azure accounts.

After the successful creation of the VM, I decided to destroy it. App Controller has sent the command to Azure and deleted the VM but left some garbage: the VM VHD disk into the blob container. Then I tried to delete the VHD via Azure Portal. The portal told me that the operation was successful but I continued to see the VHD (and to waste money on it).

After several more attempts, I decided to use PowerShell Azure commandlets and I was able to delete the VHD (and save money).

In the next days, I will investigate more on the problem. Meanwhile it is nice to know that you can continue to trust PowerShell for real management of systems.

Send a sequence of keys to an application window

This Send-Keys script send a sequence of keys to an application window.
To have more information about the key representation look at
(C)2013 Massimo A. Santin - Use it at your own risk.

.PARAMETER ApplicationTitle
The title of the application window

The sequence of keys to send

An optional number of seconds to wait after the sending of the keys

Send-Keys "foobar - Notepad" "Hello world"

Send the sequence of keys "Hello world" to the application titled "foobar - Notepad".

Send-Keys "foobar - Notepad" "Hello world" -WaitTime 5

Send the sequence of keys "Hello world" to the application titled "foobar - Notepad" 
and wait 5 seconds.

    New-Item foobar.txt -ItemType File; notepad foobar.txt ; Send-Keys "foobar - Notepad" "Hello world{ENTER}Ciao mondo{ENTER}" -WaitTime 1; Send-Keys "foobar - Notepad" "^s"

This command sequence creates a new text file called foobar.txt, opens the file using a notepad,
writes some text and saves the file using notepad.


param (


    [int] $WaitTime

# 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 {
        [return: MarshalAs(UnmanagedType.Bool)]
        public static extern bool SetForegroundWindow(IntPtr hWnd);

# get the applications with the specified title
$p = Get-Process | Where-Object { $_.MainWindowTitle -eq $ApplicationTitle }
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
    if ($WaitTime) 
        Start-Sleep -Seconds $WaitTime