Windows 7: Auto mount your VHD Disks at startup

Hi all,

Windows 7 provides some nice features including the possibility to create and attach VHD disks natively. This is a great way to organize differently your data and move it back and forth from one system to the other by solely copying your VHD file.

Unfortunately, every time you restart your machine, you loose all VHD attachments that you created before and you have to mount them manually. Martin, created a great post and explains there how to create a script that does the job for you. In this post I’m going to explain how we can automate this task with PowerShell.

In my case, I want that all VHD disks that I have in one of my directories are automatically attached on every startup. Therefore, I followed Martin’s instructions and got everything working nicely. The only drawback of this solution is the manual adding of each single VHD entry I created. That means that every time I delete or add a new disk, I have to modify the script that was created. In most cases this is not an issue, however, I wanted a more immediate and dynamic solution.

Because of that, I created a PowerShell script that takes all .vhd files in the directory it resides and attaches them by using diskpart. At the end, I only automated the step of the manual script definition in Martin’s post. The PowerShell code snipped that does the trick is described below:

foreach ($file in dir *.vhd) { 
    $command= @" 
        select vdisk file=$file 
        attach vdisk 
    "@ 
   $command | diskpart  
}

The logic is simple: loop over all files with *.vhd extension and execute the command in diskpart. Last but not least, you have to save this function in your .vhd folder and call it in your startup folder. I solved it by creating a .bat file that calls the PowerShell function like that:

powershell.exe ./AttachVHD.ps1

Every time you add a VHD disk in the directory the script resides, the script attaches automatically this disk to your system.

Martin’s blog for creating a scheduled task.

 

Hope this helps,

Patrick

This entry was posted in Developers, End-User, IT-Pro by Patrick Lamber. Bookmark the permalink.
Patrick Lamber

About Patrick Lamber

Patrick Lamber is a long-standing .NET Developer and has offered SharePoint consulting, development, and training services to customers since the launch of SharePoint 2007. He is a both a Microsoft Certified Trainer and a Microsoft Certified IT Professional, and holds Microsoft Certified Professional Development certifications for SharePoint. Patrick founded NUBO with the aim of developing a team of specialized SharePoint professionals delivering great solutions. Furthermore, in his role as SharePoint Competence Manager Patrick is also responsible for building up the SharePoint team for the company Blu Systems GmbH in Munich. Patrick speaks three languages (German, Italian, and English), meaning most of his work is focused on Italy, Germany, Austria, and Switzerland.

6 thoughts on “Windows 7: Auto mount your VHD Disks at startup

  1. Hi Shane,<br />which version of Windows are you using? Do you have Powershell enabled?<br /><br />br,<br />Patrick

  2. This is what I get.<br /><br />The string starting:<br />At D:vhdsattachvhd.ps1:2 char:14<br />+ $command= <<<< @"<br />must have the terminator: "@ at the beginning of the line.<br />At D:vhdsattachvhd.ps1:5 char:5<br />+ <<<< "@<br /> + CategoryInfo : ParserError: ( select … diskpart<br />}<br />:String) [], ParseException<br /> + FullyQualifiedErrorId : WhitespaceBeforeHereStringFooter

  3. Hi, <br />I was able to reproduce the problem. If you copy 1to1 the above script into notepad, then some whitespaces are saved into your file. This causes the exception that you specified before.<br /><br />To circumvent this problem just remove all spaces in the command before "@. Also try to avoid spaces or special characters in the names of your VHD files.<br /><br />br,<br />patrick

  4. Call CMD from powershell then use Diskpart. You should not get errors, Powershell makes the Command console very easy to control with normal functions.

  5. Again, great post. You know, I am super delighted I found this. I'll pin your blog so my friends can read your writing too!

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Powered by Sweet Captcha
Verify your real existence,
Drag the cheese to the knife
  • captcha
  • captcha
  • captcha
  • captcha