Since acquiring my MacBook Pro, I’ve been using Boot Camp extensively to be able to run either OS X or Windows on hardware as my application and performance needs dictate. I also use Virtual Box to be able to run Windows from within OS X, but most of the time, if I am developing software, my life is better running my operating system on the real hardware. I have had my machine set up to boot by default to Windows, meaning if I wanted to boot into OS X, I needed to wait patiently following a my issuing of a reboot command from either Windows or OS X for the machine to shut down and then to start back up again and hold the Option (Alt) key down while the startup sequence starts to be presented with a user interface I can use to select the disk partition from which I want to boot. I was tired of doing this and decided there had to be a better way. There is and I have found it.
In Windows, if you install the Boot Camp drivers, you have options for controlling how Boot Camp operates, including having access to a menu in the system tray with options for things like booting into OS X and a control panel for controller keyboard options and TrackPad operation. This is helpful for making the machine and the uncomfortable (in Windows) Macintosh keyboard operate in a way that is a little easier to use. The option to boot straight to OS X is what we were after and came here to find, so we could say we’re done. Good night. Thanks for coming.
No, we’re not done yet. Yes, this is an improvement over simply rebooting and having to interrupt the default boot, but we can still do a lot better.
The Boot Camp drivers also include command line support. The software installation puts an executable file on the Windows disk partition that can be used to control operation of Boot Camp from the command line. For a keyboard zealot like myself, this is the magical music I was seeking. It exists in
C:\Program Files\Boot Camp\Bootcamp.exe
Using this executable, I was able to create a PowerShell function that will set the default boot option to my OS X partition and reboot my machine. Magically, the machine comes up in the Apple Operating system (and the stands with the silver chrome sweaters stand and cheer). I added a BootCamp.ps1 script to the functions directory of my PowerShell setup. My PowerShell profile automatically dot sources all the scripts in this directory so I can use them anywhere. I also added aliases for my function.
Functions\BootCamp.ps1:
function script:bootToOsX
{
& "C:\Program Files\Boot Camp\Bootcamp.exe" -StartupDisk "Macintosh HD"
restart-computer -force
}
Aliases\BootCamp.aliasas
"reboot-osx","bootToOsX","","None"
"restart-computer-osx","bootToOsX","","None"
"osx","bootToOsX","","None""reboot-mac","bootToOsX","","None"
"restart-computer-mac","bootToOsX","","None"
"mac","bootToOsX","","None"
Thus, from PowerShell, I can boot to OS X with a single command and I can do it with one of many aliases (in case I forget what I called it. Issuing osx or mac are the shortest and easiest to remember. My PowerShell profile and all my scripts are available for reference or use at via a Bitbucket Mercurial repository.
This is great and all and enables an easy switch, but that’s not all I wanted to accomplish. It’s not enough to go from Windows to OS X. Additionally, this changes the default so that if I manually go back into Windows, subsequent reboots would land me back in OS X. It is not ideal for a software update to cause a reboot and for the machine to come back in a different operating system, no matter which is running. If I’m in OS X and an update causes a reboot, I want to boot back to OS X and I don’t want to have to do anything to make it happen. The benefit of this default switch is that now, if I’m in OS X and reboot for whatever reason (which should be done with a sudo reboot rather than clicking a button), I want to come back to OS X. Now I have that. For this to be a complete success, though, I need to be able to use the command line from OS X and make the same thing happen – set Windows as the default boot partition and reboot the machine. I have accomplished that, thanks to this answer, by using bless for setting startup disk options with this script:
reboot-windows.sh
bless -mount "/Volumes/BOOTCAMP" -legacy -setBoot
reboot
The superuser.com answer use the option –nextonly to have this change to the boot disk apply only to the next boot and leave the OS X volume as the default boot disk. This is not what I wanted, as stated earlier, so I omitted that. With this script in place, I can easily reboot to Windows from the command line and I’m able to go “There and Back Again.” When in one operating system, simple reboot commands bring me back to the same operating system and I can easily issue a command to switch which operating system that is, exactly as I wanted.