Skip to main content

How to Shrink a Disk Volume in Batch Script

Shrinking a disk volume is the process of reducing the size of a partition to create unallocated space on the disk. This unallocated space can then be used to create a new partition, for example, to install a second operating system or to create a separate drive for data. The standard command-line utility for this low-level disk operation is DISKPART.

This guide will teach you how to use DISKPART from a batch script to query and shrink a volume. It will cover the critical safety warnings you must understand, the essential commands for a "dry run" to see how much space can be reclaimed, and the final command to perform the shrink operation.

##CRITICAL WARNING: This is a Low-Level Disk Operation

Modifying disk partitions with DISKPART is powerful and carries inherent risks. A mistake can lead to irreversible data loss.

  • Always have a full backup of any important data on the volume before you begin.
  • Double- and triple-check your disk and partition numbers. Choosing the wrong one can destroy an unintended partition, including your operating system.
  • This operation is complex. If you are unsure, use the graphical "Disk Management" tool instead.
danger

You are solely responsible for the use of this command!

The Core Command: DISKPART and Scripting

DISKPART is not a standard command; it's a separate command-line environment. You cannot pipe commands into it. To automate DISKPART, you must:

  1. Create a text file containing the sequence of DISKPART commands you want to run.
  2. Execute DISKPART with the /s switch, which tells it to run the commands from your script file.

Pattern: DISKPART /s "MyDiskpartScript.txt"

The Key DISKPART Commands for Shrinking

Inside your DISKPART script, you will need a sequence of commands to select the correct volume and perform the shrink.

  • SELECT DISK <number>: Selects the physical disk (e.g., DISK 0).
  • SELECT PARTITION <number> or SELECT VOLUME <letter>: Selects the specific partition or volume (drive letter) you want to shrink.
  • SHRINK QUERYMAX: (Dry Run) This is a safe, read-only command that reports the maximum number of megabytes that can be reclaimed.
  • SHRINK DESIRED=<size>: (Execution) This is the command that performs the shrink. It will attempt to reclaim <size> megabytes of space.

Step 1 (Safety): Finding the Maximum Shrink Amount (SHRINK QUERYMAX)

Before attempting to shrink a volume, you should always perform a "dry run" to see how much free space is actually reclaimable.

CheckShrink.bat
@ECHO OFF
REM This script must be run as an Administrator.

SET "DISK_NUMBER=0"
SET "PARTITION_NUMBER=2"
SET "SCRIPT_FILE=%TEMP%\diskpart_query.txt"

ECHO --- Checking Maximum Shrink Size ---
ECHO Disk: %DISK_NUMBER%, Partition: %PARTITION_NUMBER%
ECHO.

REM --- Create the DISKPART script file ---
(
ECHO SELECT DISK %DISK_NUMBER%
ECHO SELECT PARTITION %PARTITION_NUMBER%
ECHO SHRINK QUERYMAX
) > "%SCRIPT_FILE%"

REM --- Run DISKPART with the script ---
DISKPART /s "%SCRIPT_FILE%"

REM --- Clean up the script file ---
DEL "%SCRIPT_FILE%"

This will not change your disk. It will produce output like this, which you should read carefully:

The maximum number of reclaimable bytes is:  123456 MB

Step 2 (Execution): Shrinking the Volume (SHRINK DESIRED)

Once you know how much space you can reclaim and have decided on an amount, you can create a script with the SHRINK DESIRED command.

DoShrink.bat
@ECHO OFF
REM This script must be run as an Administrator.
REM WARNING: This script modifies your disk partitions.

SET "DISK_NUMBER=0"
SET "PARTITION_NUMBER=2"
SET "SIZE_TO_SHRINK_MB=50000"
SET "SCRIPT_FILE=%TEMP%\diskpart_shrink.txt"

ECHO --- Shrinking Volume ---
ECHO WARNING: This will shrink partition %PARTITION_NUMBER% on disk %DISK_NUMBER% by %SIZE_TO_SHRINK_MB% MB.
PAUSE

(
ECHO SELECT DISK %DISK_NUMBER%
ECHO SELECT PARTITION %PARTITION_NUMBER%
ECHO SHRINK DESIRED=%SIZE_TO_SHRINK_MB%
) > "%SCRIPT_FILE%"

DISKPART /s "%SCRIPT_FILE%"
DEL "%SCRIPT_FILE%"

Common Pitfalls and How to Solve Them

Problem: The Script is Not Run as Administrator

DISKPART requires full administrative privileges to modify disk structures.

Exmaple of error message:

Access is denied.

Solution: You must run the batch script from an elevated prompt. Right-click cmd.exe or your .bat file and select "Run as administrator."

Problem: "The specified shrink size is too big..." (Unmovable Files)

This is the most common issue. You run SHRINK QUERYMAX and see you have 500 GB of free space, but it reports you can only reclaim 200 GB. This is because there are unmovable files located in the middle of the free space, preventing the file system from shrinking further.

Common unmovable files include:

  • The Hibernation file (hiberfil.sys)
  • The Page file (pagefile.sys)
  • System Restore points

Solution: This requires manual intervention before running your script.

  1. Temporarily disable hibernation: powercfg /hibernate off
  2. Temporarily disable the page file: Go to System Properties -> Advanced -> Performance Settings -> Advanced -> Virtual Memory.
  3. Temporarily disable System Restore.
  4. Reboot the computer.
  5. Run your shrink script.
  6. Re-enable the features you disabled.

Problem: Scripting DISKPART Correctly

You cannot use pipes or redirection to send commands to DISKPART interactively.

Solution: You must use the temporary script file method with the /s switch, as shown in all the examples. This is the only reliable way to automate DISKPART.

Practical Example: A Semi-Automated Shrink Script

This script helps a user by first running the QUERYMAX command and then prompting them to enter the amount they wish to shrink. This is safer as it forces a two-step process.

@ECHO OFF
SETLOCAL
REM Run as Admin.
SET "DISK_NUMBER=0"
SET "PARTITION_NUMBER=2"

:Query
ECHO --- Step 1: Querying available shrink space ---
(ECHO SELECT DISK %DISK_NUMBER% & ECHO SELECT PARTITION %PARTITION_NUMBER% & ECHO SHRINK QUERYMAX) > query.tmp
DISKPART /s query.tmp
DEL query.tmp
ECHO.

:Prompt
SET "ShrinkSize="
SET /P "ShrinkSize=Enter the amount in MB to shrink (or press Enter to cancel): "
IF NOT DEFINED ShrinkSize GOTO :EOF

ECHO.
ECHO WARNING: You are about to shrink the volume by %ShrinkSize% MB.
PAUSE

:Execute
ECHO --- Step 2: Performing shrink operation ---
(ECHO SELECT DISK %DISK_NUMBER% & ECHO SELECT PARTITION %PARTITION_NUMBER% & ECHO SHRINK DESIRED=%ShrinkSize%) > shrink.tmp
DISKPART /s shrink.tmp
DEL shrink.tmp

ECHO.
ECHO --- Shrink complete ---
ENDLOCAL

Conclusion

Automating partition shrinking with DISKPART is a powerful but high-risk operation that should be approached with extreme caution.

Key takeaways for a successful and safe operation:

  • BACK UP YOUR DATA before you begin.
  • You must run the script as an Administrator.
  • Always perform a "dry run" first with SHRINK QUERYMAX to understand your limits.
  • Use SHRINK DESIRED=<size> to perform the actual operation.
  • Be prepared to deal with unmovable files by temporarily disabling features like hibernation and the page file.
  • Automate DISKPART by writing its commands to a temporary file and running it with the /s switch.