Command Line Hard Drive Mount/Unmount in Windows XP: Batch Scripting & Power Management


2 views

Windows XP handles drive mounting differently than modern Windows versions. While you can't truly "unmount" like in Unix systems, we can achieve similar functionality through device management and power control.

The mountvol command provides basic functionality:

:: Mount a drive
mountvol X: \\?\Volume{guid}\

:: Remove drive letter (closest to unmount)
mountvol X: /D

Microsoft's Device Console utility offers more control:

:: Disable a drive (requires admin)
devcon disable *PCIIDE*IDE*Disk*

:: Re-enable later
devcon enable *PCIIDE*IDE*Disk*

Here's a complete batch script example:

@echo off
setlocal

if "%1"=="mount" (
    echo Mounting drive...
    mountvol E: \\?\Volume{12345678-1234-1234-1234-123456789ABC}\
) else if "%1"=="unmount" (
    echo Unmounting drive...
    mountvol E: /D
    :: Optional spin-down command
    powercfg -devicequery wake_from_any | find "Disk" >nul && powercfg -devicedisablewake "your_disk_name"
) else (
    echo Usage: %0 [mount|unmount]
)

When a drive is "unmounted" via these methods:

  • Drive letters are removed but physical disk remains powered
  • Automatic spin-down depends on power management settings
  • Forced spin-down requires additional tools like nircmd or custom utilities

Consider these third-party solutions for better control:

  • Diskpart scripting for partition-level control
  • psshutdown from Sysinternals for advanced power control
  • WinAPI calls through PowerShell or VBS scripts

Be aware of these limitations:

  • System/boot drives cannot be unmounted
  • Some methods may require administrator privileges
  • Improper unmounting can cause data corruption
  • Not all drives support proper spin-down commands

For persistent drive hiding (requires reboot):

REG ADD "HKLM\SYSTEM\CurrentControlSet\Services\mountmgr\NoAutoMount" /v NoAutoMount /t REG_DWORD /d 1 /f

Windows XP handles physical drives differently than modern OS versions. While it doesn't natively support "mount/unmount" terminology for entire physical drives like Unix systems, we can achieve similar functionality through diskpart and devcon utilities.

The most reliable method involves using diskpart to offline drives:

:: unmount.cmd
@echo off
diskpart /s unmount_script.txt

:: Contents of unmount_script.txt
select disk 1
offline disk

Microsoft's devcon utility provides lower-level control:

devcon disable *STOR*VEN_SEAGATE*  // Example for Seagate drives
devcon enable *STOR*VEN_SEAGATE*   // To remount

When a drive is taken offline through these methods:

  • SATA/IDE drives typically spin down after 15-30 seconds
  • SCSI drives may require additional power management commands
  • The behavior depends on the drive's firmware settings

For production use, consider this robust template:

@echo off
setlocal enabledelayedexpansion

:: Detect drive by model
for /f "tokens=2 delims==" %%a in ('wmic diskdrive get model^,index /value ^| find "ST310005"') do (
    set DRIVE_INDEX=%%a
)

if "!DRIVE_INDEX!"=="" (
    echo Drive not found
    exit /b 1
)

echo select disk !DRIVE_INDEX! > temp_script.txt
echo offline disk >> temp_script.txt

diskpart /s temp_script.txt
del temp_script.txt
  • System drives (C:) cannot be unmounted
  • Always check disk contents before operations: list volume in diskpart
  • Create restore points before modifying disk states

For systems with PowerShell 1.0/2.0 installed:

# Unmount.ps1
$disk = Get-WmiObject Win32_DiskDrive | Where-Object {$_.Model -match "ST310005"}
$disk.Disable()