Typing a string on a virtual machine

This weekend I did a bunch of virtual machine automation – and wanted to share some code snippets. The first snippet is some code that will simply type a string on a virtual machine:

$VMName = “Windows 10 Enterprise”
$VMCS = Get-WmiObject -Namespace root\virtualization\v2 -Class Msvm_ComputerSystem -Filter “ElementName=’$($VMName)'”
$keyboard = $VMCS.GetRelated(“Msvm_Keyboard”)
$keyboard.TypeText(“Hello!”) | out-null

This is a chunk of PowerShell code.  What it does is get the WMI object for the virtual machine that I am controlling; from there it gets the keyboard object for the virtual machine.  Finally it calls “TypeText” and hands in a string.

Nice and simple.  However, it should be noted that since this is literally sending keystrokes through the virtual machines keyboard – you will not see these keystrokes if you are using enhanced mode.  If you are in basic mode – then you will see everything as expected.

Cheers,
Ben