posts - 104 , comments - 115 , trackbacks - 0

Powershell: Find driveletter of mounted VHD

Not my own script, I found it on Richards powershell blog and am keeping it for future reference. Yes I look online if a script already exists before I go and find the code myself…


The script does exactly what I want so all I had to do was adjust the output with a select and format table!


function get-mountedvhdDrive {           
$disks = Get-CimInstance -ClassName Win32_DiskDrive | where Caption -eq "Microsoft Virtual Disk"           
foreach ($disk in $disks){           
$vols = Get-CimAssociatedInstance -CimInstance $disk -ResultClassName Win32_DiskPartition            
foreach ($vol in $vols){           
   Get-CimAssociatedInstance -CimInstance $vol -ResultClassName Win32_LogicalDisk |           
   where VolumeName -ne 'System Reserved'           
get-mountedvhddrive | Select DeviceID |ft -hidetableheaders

The problem with using mount-vhd is that you cannot specify a driveletter (first world problem! So that is why you would need the above if you are looking to automate tasks on VHD files. Calling the function in the way I did gives me only the driveletter so I can work with that :)

Print | posted on Wednesday, February 6, 2013 10:59 AM | Filed Under [ None ]


No comments posted yet.
Post A Comment

Powered by: