VirtualBox is a great free virtual server and can be even cooler when you run guests in Headless mode. Headless mode will allow a VM to run in the background on your machine without having the VirtualBox application windows open. Neither the VBox Manager nor the guest’s console window need be open. Here’s how to do it.
Set a system path to VirtualBox’s command line exe
- Find the path to your installation of VirtualBox. Mine is at C:\Program Files\Oracle\VirtualBox, so I will create a path to that directory.
- From Start, type in SystemPropertiesAdvanced.exe and hit enter to run the advanced system properties.
- Click the Environment Variables button.
- Under System variables, scroll down to Path and click the Edit button.
- Hit the End key to move to the end of the Variable value field, add a semi-colon ( ; ) and paste the path from step 1 after the semi-colon.
- Note that this path will open up all exe files in this directory to run from the command prompt.
- OK out of the advanced system properties. If you have any CMD windows open, you must first close them for the path to take affect.
- Open a CMD window and type vboxmanage to verify your path is set. You should see a list of commands.
Create a headless batch file to run your guest VM
The location for my VM guests is E:\VM, so my scripts point here. You will need to find the path(s) to your guests and substitute E:\VM with it including the correct .vbox guest name.
VBoxManage startvm "E:\VM\Windows\Windows.vbox" --type headless VBoxManage startvm "E:\VM\Linux\Linux.vbox" --type headless
The above lines run 2 VM guests in headless mode. You can test each of these commands in a CMD or Powershell window. In order to administer these guests, you will need to either SSH in if it’s Linux or RDP if Windows. There will be no guest VM console window. To force kill one of these, you will need to run Windows Task Manager as administrator (right-click, run as administrator). Task Manager is located under your %systemroot% directory. When running TaskMan as admin, you can mouse over the VBoxSVC.exe process and see the path to the VM. If you do not run as admin and have multiple guests running, you will not see distinguished names, i.e., you will have multiple vboxsvc.exe processes, but not be able to tell which guest each is.
Create an optional save state shutdown batch file
If you want your guests to have a clean stop in the event of a system reboot/shutdown, create a script to save the state before Windows goes down. The below script will save the state of the 2 guests from the headless script above. Note that you need to put the name of the guest in quotes below, not the path or .vbox name as in the headless script. You can get the name of the instance of the guest from mousing over your VirtualBox.exe task in TaskMan and notating the –comment section or via VirtualBox Manager.
vboxmanage.exe controlvm "Windows" savestate vboxmanage.exe controlvm "Linux" savestate
Create a scheduled task to run the above files at startup and shutdown
To bring this all together, create a scheduled Windows task to automatically start your guests at boot and save their states on shutdown.
- Open Windows Task Scheduler in Windows 8 or Scheduled Tasks in 7
- Create a new task
- Set it to run when the computer starts
- Point to your headless batch file as the program to start/run
- Complete the task, then edit its properties
- Set it to run whether logged on or not. This will allow the system to run the tasks without you even needing to login after boot.
- Set the option to allow the task to be run on demand so you can run the task manually
Create another task, but set it to run when a system event is logged. This will run when the computer receives a shutdown/reboot command. Set the options for the task as below.
- Log: System
- Source: Kernel-Power
- Event ID: 109