0

I have an external drive which, for unavoidable reasons, needs to be assigned the same letter, regardless of the computer where I plug it in to. I thought that it could be accomplished by a Diskpart script. I know the GUID of the partition, but how do I select the hard disk in Diskpart using the GUID? Or is there any other way to assign a letter to the hard disk using a script?

9
  • There doesn't appear to be a way to get from a guid to a volume. Diskpart does not appear to display guids at all, either at the partition or volume level, so even if you wrote a powershell script to select each disk, and then select each partition, you could not match it to the known GUID. The only thing I can suggest is that you attempt to match your disk based on Label (it appears to be the only identifying info you can get on a volume), and parse out the volume number. then you can use select volume n where n is the parsed volume number. that way you dont need to select disks/parts at all. May 8, 2015 at 15:28
  • Can mountvol be used to assign the letter using GUID? May 8, 2015 at 15:37
  • it looks like mountvol can use the guid, using the Volumename argument, and concatenating the GUID in, but mountvol will not mount at a drive letter, but at an existing path. you cannot specify an unmounted target path for mounting the volume. If that is fine by you, then it should work. see more here: technet.microsoft.com/en-us/library/bb490934.aspx May 8, 2015 at 15:41
  • @MycroftHolmes: Can you install a program/service on each computer the drive will be plugged into?
    – Karan
    May 8, 2015 at 21:25
  • @Karan: Installing a program would leave an unnecessary footprint on the system. I've tried to achieve the solution using the batch script I posted. It, of course, requires admin privileges though. Kindly go through the script, if you please. Any pointer to enhance the code would be appreciated. May 8, 2015 at 21:29

2 Answers 2

1

Here's the batch file I've written to change the volume letter to K. Any help to enhance the code would be greatly appreciated.

PS. There's a part of code at the beginning to run this using admin privileges. I've omitted that intentionally.

:mainBody
set "volume=\Volume{8bc9f784-9f15-11e4-be58-a60f30d14122}"
set "drive="

for %%D in (A B C D E F G H I J K L M N O P Q R S T U V W X Y Z) do (
mountvol %%D: /L | findstr "%volume%" >nul
if not errorlevel 1 (
if %%D==K (
echo DONE!
goto end
) else (
set "drive=%%D"
goto clearK
)
)
)

:clearK
mountvol K: /L >nul
if errorlevel 1 (
echo K: is free...
) else (
echo K: is taken...
for /f "tokens=1 delims=" %%A in ('mountvol K: \L') do SET currdriveguid=%%A
for %%D in ( Z Y X W V U T S R Q P O N M L K J H G F E D B A ) do (
mountvol %%D: /L >nul
if errorlevel 1 (
mountvol K: /d
mountvol %%D:\ currdriveguid
echo Current Volume moved to %%D:...
goto assignK
)
)
)


: assignK
if not defined drive (
mountvol K:\ \\?%volume%\
) else (
mountvol %drive%: /D
mountvol K:\ \\?%volume%\
)

:end
0

I also made a Powershell script to do the same job. Powershell does a better job than the batch script.

$driveI = Get-WmiObject -Class win32_volume -Filter "DriveLetter='I:'"

if ($driveI -eq $null)  {

    write-host "I: is free..."

}   elseif ($driveI.DeviceID.Contains("7899c0f7-f556-11e4-9cf2-7071bc4ab2b5") -And  "$drive.SerialNumber = '-1675536360'")    {

    write-host "I: is already assigned to the required disk..."
    Write-Host "Press any key to continue ..."
    $x = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
    exit

}   else    {    
    write-host "I: is occupied..."
    foreach ( $s in @("'Z:'", "'Y:'", "'X:'", "'W:'", "'V:'", "'U:'", "'T:'", "'R:'", "'Q:'", "'P:'", "'O:'", "'N:'", "'M:'", "'L:'", "'K:'", "'J:'", "'H:'", "'G:'", "'F:'", "'E:'", "'D:'", "'B:'", "'A:'"))
    {
        $testdrv = Get-WmiObject -Class win32_volume -Filter "DriveLetter=$s"

        if ($testdrv -eq $null)
        {
            $s = $s.Trim([char]0x0027)
            Set-WmiInstance -input $driveI -Arguments @{DriveLetter=$s}
            Write-Host I: has been moved to $s
            break
        }
    }
}

$diary = Get-WmiObject -Class win32_volume -Filter "Label='My Diary'"
if ($diary.DeviceID.Contains("7899c0f7-f556-11e4-9cf2-7071bc4ab2b5") -And  "$diary.SerialNumber = '-1675536360'")
{
    Set-WmiInstance -input $diary -Arguments @{DriveLetter="I:"}
    Write-Host "Press any key to continue ..."
    $x = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
    exit

}   else    {

    Write-Host Error Occured!
    Write-Host "Press any key to continue ..."
    $x = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
    exit
}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .