Skip to content

Instantly share code, notes, and snippets.

@badrelmers
Last active May 1, 2024 10:10
Show Gist options
  • Save badrelmers/20990cd518fed05e5f171961c63e85ba to your computer and use it in GitHub Desktop.
Save badrelmers/20990cd518fed05e5f171961c63e85ba to your computer and use it in GitHub Desktop.
portable virtualbox using the latest virtualbox installers

This makes virtualbox works in a portable mode:

  • if you have any installed virtualbox then uninstall it first
  • download https://download.virtualbox.org/virtualbox/7.0.8/VirtualBox-7.0.8-156879-Win.exe or any newer or older version
  • click on Download ZIP above to download the scripts needed
  • create a folder inside your USB/external drive or whatever, name it myVMBOX (ex. F:\myVMBOX if your USB is in F:\).
  • inside F:\myVMBOX put the virtualbox exe we just downloaded (VirtualBox-7.0.8-156879-Win.exe) and rename it to VirtualBox.exe.
  • inside F:\myVMBOX put the following files start_virtualbox.bat and uninstall_virtualbox.bat, you will find them inside the zip you just downloaded.
  • now everytime you want to start virtualbox use start_virtualbox.bat.

If you want to uninstall virtualbox use the Control Panel or uninstall_virtualbox.bat (this is optional).

That is all, now everytime you start virtualbox using start_virtualbox.bat it will start in a portable mode, the virtual machines we create will be saved to F:\myVMBOX inside your USB, and you can use the USB in any machine now, virtualbox will always find your virtual machines from your USB, but you have to always start virtualbox from start_virtualbox.bat.

If you download a newer virtualbox executable, then uninstall the old one and do the same steps again.

tip:

if you want to migrate your old created virtual machines to the new portable folder myVMBOX (for example your old virtual machines created with virtualbox), do the following:

  • start virtualbox using start_virtualbox.bat.
  • now close the virtualbox we just started.
  • open c:\myVMBOX folder (c:\myVMBOX is just a symlink shortcut to your USB folder F:\myVMBOX), you have to open c:\myVMBOX for this trick to work not F:\myVMBOX.
  • inside c:\myVMBOX create a folder and name it Machines if it does not already exist.
  • now inside c:\myVMBOX\Machines put all your old virtual machines folders, so you will have at the end something like that for example:
c:\myVMBOX\Machines\win10VM
c:\myVMBOX\Machines\win7VM
c:\myVMBOX\Machines\UbuntuVM
  • now enter to every folder inside c:\myVMBOX\Machines and doble click the file that ends with .vbox extension (it have a blue icon).

that is all, all your older virtual machines are now registred in virtualbox database and are saved inside your USB. you will find them inside your USB F:\myVMBOX.

this gist was created for this discussion: vboxme/Portable-VirtualBox#89 (comment)

@echo off
setlocal
pushd %~pd0
:: ###################################################################
:: please configure this if your VirtualBox exe have a different name
:: ###################################################################
set "VirtualBox_installer=VirtualBox.exe"
:: ###################################################################
:: from here you do not need to change anything
:: ###################################################################
set "install_dir=c:\myVirtualBox"
echo.
IF NOT EXIST "%VirtualBox_installer%" ( color 4F & echo. & echo %VirtualBox_installer% was not found, if you downloaded a new installer or changed its name then please change this script to point to the new installer, you have to change VirtualBox_installer line. & echo. & pause & exit /b )
set "portable_dir_name=myVMBOX"
set "VBOX_symlink=c:\%portable_dir_name%"
set "VBOX_Real=%~dp0%portable_dir_name%"
mkdir "%VBOX_Real%" 2>nul
rem never use rd /S it will delete the real dir content if it was created with junction.exe in xp
IF EXIST "%VBOX_symlink%" rd /Q "%VBOX_symlink%"
mklink /J "%VBOX_symlink%" "%VBOX_Real%" >nul
setx VBOX_USER_HOME c:\%portable_dir_name% >nul
set "VBOX_USER_HOME=c:\%portable_dir_name%"
IF NOT EXIST "%install_dir%\VirtualBox.exe" call :install
"%install_dir%\VBoxManage.exe" setproperty machinefolder "c:\%portable_dir_name%\Machines"
echo starting VirtualBox...
start "VirtualBox" "%install_dir%\VirtualBox.exe"
echo.
echo.
echo.
echo exiting in 7s
timeout 7
rem pause
exit /b
:install
tasklist /fi "ImageName eq VirtualBox.exe" /fo csv 2>NUL | find /I "VirtualBox.exe">NUL
if "%ERRORLEVEL%"=="0" set dirty_install=yes
tasklist /fi "ImageName eq VBoxSDS.exe" /fo csv 2>NUL | find /I "VBoxSDS.exe">NUL
if "%ERRORLEVEL%"=="0" set dirty_install=yes
tasklist /fi "ImageName eq VBoxSVC.exe" /fo csv 2>NUL | find /I "VBoxSVC.exe">NUL
if "%ERRORLEVEL%"=="0" set dirty_install=yes
IF EXIST "%ProgramFiles%\Oracle\VirtualBox\VirtualBox.exe" set dirty_install=yes
IF EXIST "%ProgramFiles(x86)%\Oracle\VirtualBox\VirtualBox.exe" set dirty_install=yes
if [%dirty_install%]==[yes] (
color 4F & echo.
echo VirtualBox is already installed or you are running a portable virtualbox.
echo only one virtualbox can run at the same time, and there should not be more
echo than one installed virtualbox. if you have virtualbox installed please
echo uninstall it first, and if you are running a Portable virtualbox then stop
echo it first, and run this script again. exiting...
echo. & pause & exit
)
echo installing VirtualBox, please wait...
"%VirtualBox_installer%" --silent --ignore-reboot --msiparams "INSTALLDIR=""%install_dir%""" VBOX_INSTALLDESKTOPSHORTCUT=0 VBOX_INSTALLQUICKLAUNCHSHORTCUT=0 VBOX_START=0
IF NOT EXIST "%install_dir%\VirtualBox.exe" ( color 4F & echo. & echo %install_dir%\VirtualBox.exe was not found, VirtualBox could not be installed. exiting... & echo. & pause & exit )
echo VirtualBox was installed
exit /b
@echo off
setlocal
pushd %~pd0
:: ###################################################################
:: please configure this if your VirtualBox exe have a different name
:: ###################################################################
set "VirtualBox_installer=VirtualBox.exe"
:: ###################################################################
:: from here you do not need to change anything
:: ###################################################################
set "install_dir=c:\myVirtualBox"
echo.
IF NOT EXIST "%VirtualBox_installer%" ( color 4F & echo. & echo %VirtualBox_installer% was not found, if you downloaded a new installer or changed its name then please change this script to point to the new installer, you have to change VirtualBox_installer line. & echo. & pause & exit /b )
tasklist /fi "ImageName eq VirtualBox.exe" /fo csv 2>NUL | find /I "VirtualBox.exe">NUL
if "%ERRORLEVEL%"=="0" set VirtualBoxrunning=yes
tasklist /fi "ImageName eq VBoxSDS.exe" /fo csv 2>NUL | find /I "VBoxSDS.exe">NUL
if "%ERRORLEVEL%"=="0" set VirtualBoxrunning=yes
tasklist /fi "ImageName eq VBoxSVC.exe" /fo csv 2>NUL | find /I "VBoxSVC.exe">NUL
if "%ERRORLEVEL%"=="0" set VirtualBoxrunning=yes
if [%VirtualBoxrunning%]==[yes] (
color 4F & echo.
echo VirtualBox is running. please stop it first. exiting...
echo. & pause & exit
)
echo uninstalling VirtualBox, please wait...
IF EXIST "%~pd0extractedinstaller" rmdir /Q /S "%~pd0extractedinstaller"
"%VirtualBox_installer%" --silent --extract --path "%~pd0extractedinstaller" >nul
pushd "%~pd0extractedinstaller"
REM FOR /f "tokens=*" %%G IN ('dir /b VirtualBox*.msi') DO start "VirtualBoxremove" /wait MsiExec.exe /norestart /uninstall "%%G" /quiet
FOR /f "tokens=*" %%G IN ('dir /b VirtualBox*.msi') DO start "VirtualBoxremove" /wait MsiExec.exe /norestart /uninstall "%%G"
popd
rmdir /Q /S "%~pd0extractedinstaller"
IF EXIST "%install_dir%\VirtualBox.exe" ( color 4F & echo. & echo %install_dir%\VirtualBox.exe was not uninstalled. run me again or uninstall it from the Control Panel & echo. & pause & exit )
setx VBOX_USER_HOME "" >nul
reg delete HKEY_CURRENT_USER\Environment /v VBOX_USER_HOME /f >nul
echo.
echo VirtualBox was uninstalled
echo.
echo.
echo.
echo exiting in 7s
timeout 7
REM pause
@badrelmers
Copy link
Author

@p060477 helllo bro,

do you think is worth to give it a try...??

i did not test it, and i will not use it because it is closed source, i prefer open source to see what they do... :)

@p060477
Copy link

p060477 commented Mar 13, 2024

@p060477 helllo bro,

do you think is worth to give it a try...??

i did not test it, and i will not use it because it is closed source, i prefer open source to see what they do... :)

thxs my dear bro,
you are right!
As always!
Thxs so much indeed for yr kind attn!
cheers!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment