Trying to create. initialize, and format VHD disks


Some background: I work in a lab environment and have a number of issues that come through that require VHDs to be created and attached to VMs for stress testing. I have come up with a script that allows the user to make the process as simple as possible, which is the following:

When I run the code, it gives me an error stating that Dismount-VHD will not work with the path given. I attempt to go in and give it the $vhdPath variable, and it still blocks out.

Another problem I’m running into is that the while statement is not incrementing $val. When it goes into the next statement, it throws an error and stops, saying that the VM in question already has the disk attached to it.

Any help would be appreciated.


I understand the beauty of pipelines in PowerShell, but this is going a little bit too far :). If you split the large pipeline, things work fine.

The main problem with what you trying to do, is that Dismount-VHD does not accept pipeline input and even if it did, it would not know what to do with a volume object (this is the output of Format-Volume)

If you want to keep the pipeline, put Dismount-VHD -Path $vhdPath on a separate line and all should be well.

Also when creating paths, you should use join-path to avoid issues.


Trying to create. initialize, and format VHD disks by licensed under CC BY-SA | With most appropriate answer!

Leave a Reply