In this post I’ll go over the process of building both Linux and Windows VMs in Oracle VirtualBox using PowerShell on a Windows host.

Prerequisites

  1. Oracle VirtualBox – I’m currently using version 6.1.40
  2. Installation media for the target OS – Examples here are with Ubuntu 20.04 (the Server image, not the Desktop one) and with Windows Server 2019 Evaluation
  3. Sufficient drive space
  4. Keep in mind that parts of the following commands (paths to ISO files, VM directory paths, and network card name) will need to be updated to match your scenario.

Building Windows VM

Open PowerShell and, to make things easier, append VirtualBox’s installation directory to the PATH environment variable.

Find the OS ID for Windows Server 2019.

PowerShell window:
PS C:\Users\Vlad> $Env:Path += "C:\Program Files\Oracle\VirtualBox\"
PS C:\Users\Vlad> VBoxManage list ostypes | Select-String -Pattern "Windows" | Select-String -Pattern "2019" -Raw
ID:          Windows2019_64
Description: Windows 2019 (64-bit)
PS C:\Users\Vlad>

SEO: VMs in VirtualBox using PowerShell

Create and register an empty Windows Server 2019 VM.

Take note of the location of the VM’s settings file’s location since that directory will also be the home of the virtual disk file(s).

PowerShell window:
PS C:\Users\Vlad> $Env:Path += "C:\Program Files\Oracle\VirtualBox\"
PS C:\Users\Vlad> VBoxManage list ostypes | Select-String -Pattern "Windows" | Select-String -Pattern "2019" -Raw
ID:          Windows2019_64
Description: Windows 2019 (64-bit)
PS C:\Users\Vlad> VBoxManage createvm --name WinSrv2k19 --ostype Windows2019_64 --register
Virtual machine 'WinSrv2k19' is created and registered.
UUID: 3577e1ee-9e0e-4607-a41c-d5db82daf7d2
Settings file: 'F:\VirtualBoxVMs\WinSrv2k19\WinSrv2k19.vbox'

SEO: VMs in VirtualBox using PowerShell

Add CPU cores, RAM, and VRAM. Set the graphics controller, and set the network type as bridged as well specifying the name of the host’s NIC that will be used.

Add a SATA storage controller, specify the controller’s chipset, the port count, and set it as boot-able.

Create the virtual disk file that will eventually become the C drive.

Add a second virtual disk file (optional).

Attach the two disks to the storage controller.

Attach the Windows Server 2019 Evaluation ISO.

Start the VM.

And proceed with the OS install process as described here.

Building an Ubuntu VM

Open PowerShell and, to make things easier, append VirtualBox’s installation directory to the PATH environment variable.

Find the OS ID for Ubuntu 64bit.

PowerShell window:
PS C:\Users\Vlad> $Env:Path += "C:\Program Files\Oracle\VirtualBox\"
PS C:\Users\Vlad> VBoxManage list ostypes | Select-String -Pattern "ubuntu" -Raw
ID:          Ubuntu
Description: Ubuntu (32-bit)
ID:          Ubuntu_64
Description: Ubuntu (64-bit)
PS C:\Users\Vlad>


SEO: VMs in VirtualBox using PowerShell

Create and register an empty Ubuntu 64bit VM.

Take note of the location of the VM’s settings file’s location since that directory will also be the home of the virtual disk file(s).

PowerShell window:
PS C:\Users\Vlad> $Env:Path += "C:\Program Files\Oracle\VirtualBox\"
PS C:\Users\Vlad> VBoxManage list ostypes | Select-String -Pattern "ubuntu" -Raw
ID:          Ubuntu
Description: Ubuntu (32-bit)
ID:          Ubuntu_64
Description: Ubuntu (64-bit)
PS C:\Users\Vlad> VBoxManage createvm --name Ubuntu --ostype Ubuntu_64 --register
Virtual machine 'Ubuntu' is created and registered.
UUID: 67ecc358-97e9-4c40-953d-348632f24868
Settings file: 'F:\VirtualBoxVMs\Ubuntu\Ubuntu.vbox'


SEO: VMs in VirtualBox using PowerShell

Add CPU cores, RAM, and VRAM. Set the graphics controller, and set the network type as bridged as well specifying the name of the host’s NIC that will be used.

Add a SATA storage controller, specify the controller’s chipset, the port count, and set it as boot-able.

Add an IDE storage controller, for CD/DVD images.

Create the virtual disk file.

Attach the virtual disk file to the SATA storage controller.

Attach the ISO to the IDE controller.

Start the VM.

And proceed with the OS install process as described here.

Conclusion

While building VMs from PowerShell might seem intimidating if you’re not used with the command line, the process isn’t really that intricate and can end up saving you a lot of time if you tear down and build new VMs fairly often.

If you’re curios about the Windows Terminal theme that makes the above screenshots so easy to read: it’s called Retrowave, by Chrissy LeMaire, and you can find it here.