{"id":63,"date":"2010-06-10T13:48:51","date_gmt":"2010-06-10T19:48:51","guid":{"rendered":"http:\/\/localhost\/todd_wp\/automating_windows_7_boot_entry_creating_with_powershell\/"},"modified":"2013-07-09T11:39:14","modified_gmt":"2013-07-09T17:39:14","slug":"automating_windows_7_boot_entry_creating_with_powershell","status":"publish","type":"post","link":"https:\/\/toddbaginski.com\/blog\/automating_windows_7_boot_entry_creating_with_powershell\/","title":{"rendered":"Automating Windows 7 Boot Entry Creation With PowerShell"},"content":{"rendered":"<p>&nbsp;<\/p>\n<p>Recently I needed to create an automated process to make boot entries for developer lab machines to allow them to boot to a virtual hard disk (VHD). The main requirements were to make the process simple and easily configurable. In this blog post I\u2019ll show you the PowerShell script and accompanying XML settings file I used to get the job done.<\/p>\n<p><strong>Configuration file<\/strong><\/p>\n<p>The following XML configuration file (settings.xml) stores the settings for the boot entry. It allows you easily configure where the VHD is located and the description you would like to give to the boot entry. The Drive, Path, and Description values are extracted by the PowerShell script and used to configure the new boot entry.<\/p>\n<div id=\"codeSnippetWrapper\">\n<div id=\"codeSnippet\" style=\"text-align: left; line-height: 12pt; background-color: #f4f4f4; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;\">\n<pre style=\"text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;\"><span style=\"color: #0000ff;\">&lt;?<\/span><span style=\"color: #800000;\">xml<\/span> <span style=\"color: #ff0000;\">version<\/span><span style=\"color: #0000ff;\">=\"1.0\"<\/span> <span style=\"color: #ff0000;\">encoding<\/span><span style=\"color: #0000ff;\">=\"utf-8\"<\/span>?<span style=\"color: #0000ff;\">&gt;<\/span><\/pre>\n<p><!--CRLF--><\/p>\n<pre style=\"text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;\"><span style=\"color: #0000ff;\">&lt;<\/span><span style=\"color: #800000;\">Settings<\/span><span style=\"color: #0000ff;\">&gt;<\/span><\/pre>\n<p><!--CRLF--><\/p>\n<pre style=\"text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;\">    <span style=\"color: #0000ff;\">&lt;<\/span><span style=\"color: #800000;\">BootEntry<\/span> <span style=\"color: #ff0000;\">Drive<\/span><span style=\"color: #0000ff;\">=\"C\"<\/span> <span style=\"color: #ff0000;\">Path<\/span><span style=\"color: #0000ff;\">=\"VHDsSP2010.VHD\"<\/span> <span style=\"color: #ff0000;\">Description<\/span><span style=\"color: #0000ff;\">=\"Boot To SP 2010 VHD\"<\/span> <span style=\"color: #0000ff;\">\/&gt;<\/span><\/pre>\n<p><!--CRLF--><\/p>\n<pre style=\"text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;\"><span style=\"color: #0000ff;\">&lt;\/<\/span><span style=\"color: #800000;\">Settings<\/span><span style=\"color: #0000ff;\">&gt;<\/span><\/pre>\n<p><!--CRLF--><\/p>\n<\/div>\n<\/div>\n<p><strong>PowerShell Script<\/strong><\/p>\n<div>\n<div>The full PowerShell Script can be downloaded <a href=\"https:\/\/toddbaginski.com\/blog\/files\/Article Files\/CreateBootEntryPowerShellScript.zip\" target=\"_blank\">here<\/a>. The following portions of the script do the heavy lifting.<\/div>\n<div><\/div>\n<div>\n<div>First, the PowerShell script returns the values from the settings.xml file.<\/div>\n<div><\/div>\n<div id=\"codeSnippetWrapper\">\n<pre id=\"codeSnippet\" style=\"text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;\">[System.Xml.XmlElement]$settings = Get-Settings\r\n$BootEntry = $settings.BootEntry\r\n$Drive = $BootEntry.Drive\r\n$Path = $BootEntry.Path\r\n$Description = $BootEntry.Description<\/pre>\n<p>&nbsp;<\/p>\n<\/div>\n<p>Then it copies the current boot entry and assigns it the description from the settings.xml file.<\/p>\n<div id=\"codeSnippetWrapper\">\n<pre id=\"codeSnippet\" style=\"text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;\">$BootEntryCopy = bcdedit \/copy <span style=\"color: #006080;\">'{current}'<\/span> \/d $BootEntry.Description<\/pre>\n<p>&nbsp;<\/p>\n<\/div>\n<p>Then the script extracts the GUID for the new boot entry.<\/p>\n<div id=\"codeSnippetWrapper\">\n<pre id=\"codeSnippet\" style=\"text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;\">$CLSID = $BootEntryCopy | ForEach-Object {$_.Remove(0,37).Replace(<span style=\"color: #006080;\">\".\"<\/span>,<span style=\"color: #006080;\">\"\"<\/span>)}<\/pre>\n<p>&nbsp;<\/p>\n<\/div>\n<p>Then it uses the values from the settings.xml file to create the path to the VHD which <strong>bcdedit<\/strong> uses to modify the boot entry.<\/p>\n<div id=\"codeSnippetWrapper\">\n<pre id=\"codeSnippet\" style=\"text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;\">$VHD = <span style=\"color: #006080;\">'vhd=['<\/span> + $Drive + ':]' + $Path<\/pre>\n<p>&nbsp;<\/p>\n<\/div>\n<p>Finally, the script calls <strong>bcdedit<\/strong> to configure the boot entry.<\/p>\n<div id=\"codeSnippetWrapper\">\n<pre id=\"codeSnippet\" style=\"text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;\">bcdedit \/set $CLSID device $VHD\r\nbcdedit \/set $CLSID osdevice $VHD\r\nbcdedit \/set $CLSID detecthal on<\/pre>\n<p>&nbsp;<\/p>\n<\/div>\n<p>The script output looks like this when it is run. (Your GUID will differ from the screenshot.)<\/p>\n<p><a href=\"https:\/\/toddbaginski.com\/blog\/images\/www_toddbaginski_com\/blog\/WindowsLiveWriter\/AutomatingWindows7BootEntryCreatingWithP_11298\/image_thumb.png\" rel=\"lightbox\"><img loading=\"lazy\" decoding=\"async\" style=\"display: inline; border-width: 0px;\" title=\"PowerShell Ouput\" src=\"https:\/\/toddbaginski.com\/blog\/images\/www_toddbaginski_com\/blog\/WindowsLiveWriter\/AutomatingWindows7BootEntryCreatingWithP_11298\/image_thumb.png\" alt=\"PowerShell Ouput\" width=\"605\" height=\"521\" border=\"0\" \/><\/a><\/p>\n<p>Enjoy the script, I hope it helps you out!<\/p>\n<p>Thanks to my buddy <a href=\"http:\/\/stsadm.blogspot.com\" target=\"_blank\">Gary Lapointe<\/a> for showing me how to make XML settings files with PowerShell!<\/p>\n<p>Thanks to <a href=\"http:\/\/blogs.msdn.com\/b\/cmayo\" target=\"_blank\">Chris Mayo<\/a> for showing me how to use<strong> BCDEDIT<\/strong> to make custom boot entires. His blog series that describes how to make bootable VHDs is excellent.<\/p>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>&nbsp; Recently I needed to create an automated process to make boot entries for developer lab machines to allow them to boot to a virtual hard<span class=\"excerpt-hellip\"> [\u2026]<\/span><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[89],"tags":[5,8],"class_list":["post-63","post","type-post","status-publish","format-standard","hentry","category-operations","tag-helpful-code","tag-virtual-environments"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/toddbaginski.com\/blog\/wp-json\/wp\/v2\/posts\/63","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/toddbaginski.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/toddbaginski.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/toddbaginski.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/toddbaginski.com\/blog\/wp-json\/wp\/v2\/comments?post=63"}],"version-history":[{"count":5,"href":"https:\/\/toddbaginski.com\/blog\/wp-json\/wp\/v2\/posts\/63\/revisions"}],"predecessor-version":[{"id":927,"href":"https:\/\/toddbaginski.com\/blog\/wp-json\/wp\/v2\/posts\/63\/revisions\/927"}],"wp:attachment":[{"href":"https:\/\/toddbaginski.com\/blog\/wp-json\/wp\/v2\/media?parent=63"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/toddbaginski.com\/blog\/wp-json\/wp\/v2\/categories?post=63"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/toddbaginski.com\/blog\/wp-json\/wp\/v2\/tags?post=63"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}