How to Mount a Share Folder in all my VirtualBox Virtual Machines
3 min readSep 8, 2020
In this quick story, I will show a simple PowerShell script to mount a Share Folder in all my Oracle VirtualBox Virtual Machines.
Mount a Shared Folder Script
First, we define the variables:
$VirtualBox = "C:\Program Files\Oracle\VirtualBox\VBoxManage"
$Name = "Tools"
$SharedFolder = "C:\Tools"
We get the list of virtual machines and redirect output to a file:
Start-Process -NoNewWindow -Wait -FilePath $VirtualBox -Argument "list vms" -RedirectStandardOutput "vmlistSource.txt"
Then, we extract the VM ID and create a list of VM IDs:
$vmlistSource = get-content "vmlistSource.txt"
$vmlist = $vmlistSource | foreach {
$vm = $_.split(" ").Replace("{","").Replace("}","")
write-output $vm[1]
}
Finally, we add the Shared Folder to the VM:
$vmlist | foreach {
$parameter = "sharedfolder add " + $_ + " --name " + $Name + " --hostpath " + $SharedFolder + " --automount"
Start-Process -NoNewWindow -Wait -FilePath $VirtualBox -Argument $parameter
}
This is the full script to mount a shared drive in all our VirtualBox virtual machines.