Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Starts a VirtualBox Virtual Machine with the maximum possible number of virtual monitors.
# Starts the specified VirtualBox virtual machine with the maximum number
# of virtual monitors (i.e. with the number of virtual monitors matching the
# number of physical monitors attached to the PC).
# Set these variables as appropriate for your environment.
$virtualboxBinFolder = "C:\Program Files\Oracle\VirtualBox"
$VMName = "MyVirtualMachine"
# Returns the physical monitor count for this PC.
Function GetMonitorCount
{
# See http://stackoverflow.com/questions/7967699/get-screen-resolution-using-wmi-powershell-in-windows-7
Add-Type -AssemblyName System.Windows.Forms
$allScreens = [System.Windows.Forms.Screen]::AllScreens
return $allScreens.Length
}
# Sets the virtual monitor count on the specified VirtualBox virtual machine,
# and then starts the machine.
# Assumes the $virtualboxBinFolder global variable points to VirtualBox's bin folder.
Function StartVMWithMonitorCount ($VMName, $monitorCount)
{
& "$virtualboxBinFolder\VBoxManage" modifyvm $VMName --monitorcount $monitorCount
& "$virtualboxBinFolder\VBoxManage" startvm $VMName
}
StartVMWithMonitorCount $VMName (GetMonitorCount)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment