Managing the Default Virtual Machine Location with Windows Virtual PC






Virtual PC 2004 / 2007 always default to creating new virtual machines in the current users “My Documents” folder.  Users who need to use an alternate location can do so by setting up the MYVIRTUALMACHINES environment variable.


Things have changed significantly with Windows Virtual PC.


The first change is that we no longer look at the MYVIRTUALMACHINES environment variable.


The second change is that we no longer enforce a static default location.  Rather – every time you create a new virtual machine – the location that you specify for the new virtual machine:


image


Will be used as the default virtual machine location the next time you try to create a virtual machine.


Finally – if you do want to programmatically set a default location you can do so using some simple scripts:


PowerShell:



param([string]$path)
 
# Check for correct command-line arguments
If ($path -eq “”)
 {
 write-host “Missing command-line argument.”
 write-host “USage: SetDefaultVMPath.ps1 -path `”Default path to use for new virtual machines`“”
 exit
 }
 
# Connect to Virtual PC
$vpc=new-object –com VirtualPC.Application –Strict
 
# Set the new default VM path
$vpc.DefaultVMConfigurationPath = $path

VBScript:



Option Explicit
 
Dim namedArguments, vpc, defaultVMPath
 
‘ Check that the script is running at the command line.
If UCase(Right(Wscript.FullName, 11)) = “WSCRIPT.EXE” Then
 WScript.Echo “This script must be run under CScript.”
 WScript.Quit
End If
 
‘ Get the new default VM path from the command-line arguments
Set namedArguments = WScript.Arguments.Named
 
If namedArguments.Exists(“path”) Then
 defaultVMPath = namedArguments.Item(“path”)
Else
 WScript.Echo “Missing command-line argument”
 WScript.Echo
 WScript.Echo “Usage: SetDefaultVMPath.vbs /path:” & chr(34) & “Default path to use for new virtual machines” & chr(34)
 WScript.Echo
 WScript.Quit
End If
 
‘ Attempt to connect to Virtual PC
On Error Resume Next
Set vpc = CreateObject(“VirtualPC.Application”)
If Err.Number <> 0 Then
 WScript.Echo “Unable to connect to Virtual PC.”
 WScript.Quit
End if
On Error Goto 0
 
‘ Set the new default VM path
vpc.DefaultVMConfigurationPath = defaultVMPath
 
WScript.Echo

 


 


Note that while these scripts will set the default location – if the user chooses a different location when creating a new virtual machine, that location will become the new default virtual machine location. 


This means that these scripts are only really useful for customizing an initial deployment of Windows Virtual PC.  Alternatively you could run these scripts on logon to set the default path back to your desired location.


Cheers,
Ben

SetDefaultVMPathsScripts.zip