How to Start/Stop VBOX Virtual Machines with Batch Files on Windows

VBOX

Managing virtual machines can be a breeze if you have the right tools and know-how. In this guide, we’ll show you how to start and stop your virtual machines using batch files on a Windows operating system. This method is particularly useful if you’re using Oracle VM VirtualBox.

Prerequisites

Before we begin, make sure you have the following:

  • A Windows Operating System
  • Oracle VM VirtualBox installed
  • A virtual machine (VM) setup in Oracle VM VirtualBox

Creating a Batch File to Start Your Virtual Machine

Starting your virtual machine with a batch file is simple. Follow these steps:

  • Open Notepad: You can open Notepad by searching for it in the Start menu.
  • Enter the Code: Copy and paste the following code into Notepad:
@echo off
cd /d "C:\Program Files\Oracle\VirtualBox"
VBoxManage.exe controlvm "<VIRTUAL MACHINE NAME>" poweroff
  • Replace <VIRTUAL MACHINE NAME>: Replace <VIRTUAL MACHINE NAME> with the actual name of your virtual machine. For example, if your VM is named “Ubuntu,” the code should look like this:
@echo off
cd /d "C:\Program Files\Oracle\VirtualBox"
VBoxManage.exe startvm "Ubuntu" --type headless
  • Save the File: Save the file with a .bat extension. For instance, you can save it as Start_VM.bat.

Explanation

  • @echo off: This command prevents the commands in the batch file from being displayed in the command prompt window.
  • cd /d "C:\Program Files\Oracle\VirtualBox": This changes the directory to where VirtualBox is installed.
  • VBoxManage.exe startvm "Ubuntu" --type headless: This command starts the virtual machine in headless mode, meaning it runs in the background without a GUI.

Connecting to Your VM

Since the VM starts in headless mode, you need to connect to it using an SSH client like Putty or Bitvise SSH Client.

Creating a Batch File to Stop Your Virtual Machine

To stop your virtual machine, you can create another batch file:

  • Open Notepad: Open a new Notepad window.
  • Enter the Code: Copy and paste the following code into Notepad:
@echo off
cd /d "C:\Program Files\Oracle\VirtualBox"
VBoxManage.exe controlvm "<VIRTUAL MACHINE NAME>" poweroff
  • Replace <VIRTUAL MACHINE NAME>: Replace <VIRTUAL MACHINE NAME> with the name of your virtual machine. For example, if your VM is named “Ubuntu,” the code should look like this:
@echo off
cd /d "C:\Program Files\Oracle\VirtualBox"
VBoxManage.exe controlvm "Ubuntu" poweroff
  • Save the File: Save the file with a .bat extension, such as Stop_VM.bat.

Additional Commands

You can also use other commands with controlvm to perform different actions on your VM:

  • pause: Pause the VM.
VBoxManage.exe controlvm "Ubuntu" pause
  • resume: Resume a paused VM.
VBoxManage.exe controlvm "Ubuntu" resume
  • reset: Reset the VM.
VBoxManage.exe controlvm "Ubuntu" reset
  • savestate: Save the current state of the VM.
savestate: Save the current state of the VM.

Using batch files to manage your virtual machines can save you time and simplify your workflow. By following the steps above, you can easily start and stop your VMs using simple scripts. This method not only enhances your productivity but also ensures that you have more control over your virtual environment.