Managing VirtualBox with PowerShell

In my line of work I simply can’t afford not to use virtualization, and I use just about all the major tools from time to time. But most of the time I rely on the free VirtualBox program from Oracle. One of the reasons I like it is it’s relatively low footprint. It is not as full featured as a product like VMware and I don’t want to get into the merits of different virtualization products. Instead I want to show you how I’m managing my virtual machines. VirtualBox does not have any PowerShell cmdlets, but there is a COM object so I wrote a few PowerShell functions around it and bundled everything into a module.

This is definitely still a work in progress but I felt I had achieve the majority of the functionality I was after. I can now get, start, stop and suspend virtual machines. I also included a function to retrieve all VirtualBox related processes.

[cc lang=”DOS”]

PS S:\> get-command -Module PSVirtualBox

CommandType Name Definition
———– —- ———-
Function Get-VBoxMachine …
Function Get-VBoxProcess …
Function Get-VirtualBox …
Alias gvb Get-VirtualBox
Alias gvbm Get-VBoxMachine
Alias gvbp Get-VBoxProcess
Function Start-VBoxMachine …
Alias stavbm Start-VBoxMachine
Function Stop-VBoxMachine …
Alias stovbm Stop-VBoxMachine
Function Suspend-VBoxMachine …
Alias suvbm Suspend-VBoxMachine

All of the functions have comment based help and there is also an About file included in the module, About_PSVirtualBox. Just about all of the functionality is derived from the main COM object which has a ProgID of VirtualBox.VirtualBox. The module has a function called Get-VirtualBox that creates this object. When you import the module, it also creates a global variable, $vbox. The other functions re-use this object. If it doesn’t exist when you call a function like Suspend-VBoxMachine, it will be recreated.

[cc lang=”PowerShell”]

if (-Not $global:vbox) {

You can use the $vbox object to invoke other methods or properties that I haven’t wrapped up in PowerShell yet. And speaking of COM objects, the Get-VBoxMachine command writes a custom object to the pipepline with what I felt were commonly used properties. But you can add a property with the raw or native COM object by using -IncludeRaw.

[cc lang=”DOS”]
PS C:\> Get-VBoxMachine -Name CoreDC01 -IncludeRaw

ID : ed29417c-869a-45bf-bbf3-79a407ade630
Name : CoreDC01
MemoryMB : 512
Description :
State : Running
OS : Windows2008_64
Raw : System.__ComObject

PS C:\> Get-VBoxMachine -Name CoreDC01 -IncludeRaw | Select -ExpandProperty Raw | Select SessionType


The last thing to be aware of if you use this module is that virtual machine names are case sensitive. But the functions are designed to work together in the pipeline so you don’t have to worry too much.

[cc lang=”DOS”]
PS C:\> Get-VBoxMachine | Stop-VBoxMachine -whatif
What if: Performing operation “Stop-VBoxMachine” on Target “Win2008 R2 Standard”.
What if: Performing operation “Stop-VBoxMachine” on Target “CoreDC01″.
What if: Performing operation “Stop-VBoxMachine” on Target “Ubuntu”.

I have a few more features planned and if there is something you think should be added, I hope you’ll let me know.

Download the PSVirtualBox module.

4 thoughts on “Managing VirtualBox with PowerShell”

Comments are closed.