Mount and dismount hard drive through a script/software

193,515

Solution 1

DISCLAIMER: You are responsible for your own actions. I AM NOT responsible for any damage that you could possibly cause to your computer or laptop by trying anything outlined below.

Upon doing some research, I found THIS Instructable, detailing how to mount and unmount drives using the Windows command MOUNTVOL.

Tutorial

  1. First, open Command Prompt as Administrator.

  2. Run the command mountvol and take note of the volume name above the drive letter that you want to mount/unmount (e.g. \\?\Volume{########-####-####-####-############}\ )

  3. To unmount a drive, type mountvol [DriveLetter] /p. Be sure to replace [DriveLetter] with the letter assigned to the drive you wish to unmount. (For example, G:)

  4. To mount a drive, type mountvol [DriveLetter] [VolumeName]. Make sure you replace [DriveLetter] with the letter you wish to mount the drive to (for example, G:), and [VolumeName] with the volume name you noted in Step 2.

Batch Script

This is an example of two simple batch scripts you could use to mount and unmount the drive of your choice.

In order to make the batch files work, you will need to run them with administrative privileges, or the batch file will return an Access Denied error.

Unmounting:

@echo off

REM Be sure to change this to the drive you want to unmount! 
set drive=G:

echo Unmounting Drive...
mountvol %drive% /p
echo Drive Unmounted!

pause
exit

Mounting:

@echo off

REM Be sure to change this to the drive letter you want to mount the drive to!
set drive=G

REM Be sure to change this to the Volume Name of the drive you want to mount!
set volume=\\?\Volume{ae101d9f-7653-11e3-be83-8056f23387a6}\

:start
echo Mounting Drive...
mountvol %drive%: %volume%
echo Drive Mounted!
 
pause
exit

Make sure you alter the batch files at the top before running them, to make sure you are mounting/unmounting the correct drive.

Do NOT use /D, which only removes the drive letter assignment. Use /p. From mountvol /?:

/p          Removes the volume mount point from the specified directory,
            dismounts the volume, and makes the volume not mountable.
            You can make the volume mountable again by creating a volume
            mount point.

Solution 2

Try this. I do not know whether this is what you want.

  1. Start diskpart.

    • Search for diskpart in the Start Menu or open Command Prompt and type in diskpart. You need administrative privileges to run diskpart.
  2. Type list volume in diskpart.

    • Note the volume number and name for the volume to be removed.
  3. Type select volume [drive letter (or drive number)].

    • For example: select volume G or select volume 5.
  4. Type remove letter [volume letter].

    • For example: remove letter G.

DONE!

To mount the volume, try the following:

  1. Follow the 1st and 2nd steps. Note that you can see the volume number and other details only, not the volume letter.

  2. Type assign letter [volume letter] or just assign. In here, replace the [volume letter] to any letter which you want to assign to the volume.

Solution 3

From powershell we can mount/dismount via WMI methods.

Get-WmiObject -class Win32_Volume | where-object {$_.DeviceID -Like "\\?\Volume{########-####-####-####-############}\"} | foreach-object -process {$_.AddMountPoint("X:")}

To dismount, maybe this.

Get-WmiObject -class Win32_Volume | where-object {$_.DeviceID -Like "\\?\Volume{########-####-####-####-############}\"} | foreach-object -process {$_.Dismount()}

and here is how to find the GUID from powershell. Don't forget to edit the samples.

Get-WmiObject -class Win32_Volume | Select-Object DeviceID,DriveLetter 

Solution 4

You can use diskpart command line utility. It has "own" command line that accepts diskpart commands.

Here is article about this tool: link to technet.microsoft.com

You have to:

  • run diskpart.exe (it comes with Windows Vista/7/8, maybe some older too)
  • select disk with select disk n command (where n is disk number)
  • select partition with select partition n command (where n is partition number)
  • run assign command with parameters described below

assign [{letter=d|mount=path}] [noerr]

Assigns a drive letter or mount point to the volume with focus. If no drive letter or mount point is specified, then the next available drive letter is assigned. If the assigned drive letter or mount point is already in use, an error is generated.

By using the assign command, you can change the drive letter associated with a removable drive. You cannot assign drive letters to system volumes, boot volumes, or volumes that contain the paging file. You cannot assign a drive letter to an OEM partition or any GPT partition other than a basic data partition.

letter=d Specifies the drive letter that you intend to assign to the volume.

mount=path Specifies the mount point path that you intend to assign to the volume.

You can also use list disk, list partition or list volume to find your disk/partition/volume number.

Be careful what are you doing with that tool. It may be used to format drives.

Share:
193,515
Yahya Essam
Author by

Yahya Essam

Updated on September 18, 2022

Comments

  • Yahya Essam
    Yahya Essam almost 2 years

    is there a way to mount and dismount a connected harddrive through a script or a simple utility software in Windows 8.1?

    Basically, I have a hard drive in the ultrabay slot of my ThinkPad (instead of the dvd-drive). When booting the system, this hard drive is automatically being mounted and visible to me. Using the icon in the taskbar I can unmount it. Unfortunately, it is then only available again when I reboot the system.

    So I wanted to ask if there is any command line script I can execute to dismount the drive and mount it again with another script without having to reboot?

    This way I could call the unmount-script after booting, so the drive isn't always running, only when I need some stuff from it, I call the mount-script and then can access the files.

    Would be great if anybody has any ideas on how to solve this! Thank you!

  • itsmejoeeey
    itsmejoeeey over 10 years
    Did this work? Does it suit your needs?
  • bwDraco
    bwDraco over 9 years
    Please expand upon this answer—link-only answers aren't particularly useful and can become invalid altogether if the linked content becomes unavailable.
  • fixer1234
    fixer1234 over 9 years
    Also, a link to general subject matter discussion doesn't really answer the question, it is just a suggestion for a place to start looking for an answer. For those kinds of links, it's particularly important to include the relevant portion in your answer. This kind of helpful suggestion should go in a comment. It looks like your answer may be deleted before you return from the holidays, so I'll move the info to a comment so that it isn't lost.
  • Kamil
    Kamil over 9 years
    Sorry guys, I thought that single "diskpart" word could be an answer. I added more details.
  • Kamil
    Kamil over 9 years
    I didn't expected that improving post will cause downvotes :)
  • Costin Gușă
    Costin Gușă about 9 years
    diskpart CANNOT unmount volumes
  • Costin Gușă
    Costin Gușă about 9 years
    please also see superuser.com/questions/295913/… - there are two methods, mountvol and devcon
  • KansaiRobot
    KansaiRobot over 6 years
    @joejoe31b apologies for a very naive question but, is there a way to use something other than the complicated GUID? Also is there a way to identify which are internal HDD and other types of storage?
  • QMaster
    QMaster over 4 years
    What about dismounting EFI partition? Are you sure about using /p switch with that?
  • Zimba
    Zimba over 3 years
    diskpart doesn't accept commands in CMD; it spawns it's own window for command input