How to Set the Hibernate File Size in Batch Script
The Windows hibernation file (hiberfil.sys) is a reserved system file stored at the root of the system drive. Its purpose is to hold the complete contents of physical memory (RAM) when the computer enters hibernation or Fast Startup mode. By default, this file can consume a significant portion of your disk space, often 6 GB, 12 GB, or even 32 GB on machines with large amounts of RAM.
In this guide, we will explore how to adjust the size of hiberfil.sys using Batch Script, including setting custom percentages, switching between full and reduced types, and understanding the implications each setting has on system functionality.
Understanding Hibernation File Types
Windows supports two hibernation file types, each with a different purpose and size:
| Type | Default Size | Purpose |
|---|---|---|
full | ~75% of installed RAM | Supports both full Hibernation and Fast Startup |
reduced | ~40% of installed RAM | Supports only Fast Startup (not full Hibernation) |
- Full: The file is large enough to store the entire contents of RAM. This is required if you want the "Hibernate" option in the Start Menu.
- Reduced: The file is smaller, storing only the kernel session data needed for Fast Startup. Full hibernation is unavailable in this mode.
Method 1: Setting the File Type (Full or Reduced)
The simplest way to manage the file size is to switch between the two preset types.
Switching to Reduced (Save Disk Space)
@echo off
setlocal
:: Verify Admin
net session >nul 2>&1
if %errorlevel% neq 0 (
echo [ERROR] Administrator privileges required.
pause
exit /b 1
)
echo Switching hibernation file to REDUCED size...
:: Ensure hibernation is enabled first
powercfg /h on >nul 2>&1
:: Set to reduced type
powercfg /h /type reduced
if %errorlevel% == 0 (
echo [SUCCESS] Hibernation file set to reduced.
echo Fast Startup remains available.
echo Full Hibernate is now unavailable from the Start Menu.
) else (
echo [ERROR] Failed. The system may not support hibernation.
)
pause
Switching to Full (Enable Complete Hibernation)
@echo off
setlocal
net session >nul 2>&1
if %errorlevel% neq 0 (
echo [ERROR] Admin required.
pause
exit /b 1
)
echo Switching hibernation file to FULL size...
powercfg /h on >nul 2>&1
powercfg /h /type full
if %errorlevel% == 0 (
echo [SUCCESS] Hibernation file set to full.
echo Both Hibernate and Fast Startup are available.
) else (
echo [ERROR] Failed to set full type.
)
pause
Method 2: Setting a Custom Percentage
If neither the default "full" (75%) nor "reduced" (40%) sizes suit your needs, you can specify an exact percentage of installed RAM using the /size parameter.
@echo off
setlocal
net session >nul 2>&1
if %errorlevel% neq 0 (
echo [ERROR] Admin required.
pause
exit /b 1
)
set "percent=50"
echo =========================================
echo Setting hibernation file to %percent%%% of RAM
echo =========================================
:: Enable hibernation first
powercfg /h on >nul 2>&1
if errorlevel 1 (
echo [ERROR] Failed to enable hibernation.
pause
exit /b 1
)
:: Set size
powercfg /h /size %percent%
set "err=%errorlevel%"
if "%err%"=="0" (
echo [SUCCESS] Hibernation file size set to %percent%%%.
) else (
echo [ERROR] Failed to set size.
echo Minimum is 40%%, maximum is 100%%.
)
pause
Size Constraints
- Minimum: 40% of installed RAM. Values below this will produce an error.
- Maximum: 100% of installed RAM.
- Recommended for full hibernation: At least 75%. Below this, hibernation may fail if RAM usage is high at the time of hibernation.
- Recommended for Fast Startup only: 40% is sufficient.
The actual disk space consumed depends on your total installed RAM. On a machine with 16 GB of RAM, setting /size 50 creates an 8 GB hiberfil.sys file. On a machine with 32 GB RAM, the same percentage produces a 16 GB file.
Method 3: Interactive Size Manager
For IT technicians who need to quickly adjust the size during machine setup:
@echo off
title Hibernation File Size Manager
setlocal enabledelayedexpansion
net session >nul 2>&1
if %errorlevel% neq 0 (
echo [ERROR] Run as Administrator.
pause
exit /b 1
)
:menu
cls
echo =============================================
echo HIBERNATION FILE SIZE MANAGER
echo =============================================
echo.
:: Get total physical memory via WMI
for /f "tokens=2 delims==" %%A in ('wmic computersystem get TotalPhysicalMemory /value ^| find "="') do (
for /f "delims=" %%B in ("%%A") do set "ram_bytes=%%B"
)
:: Convert to GB (approximate)
set /a ram_gb=!ram_bytes:~0,-9!
echo Installed RAM: ~!ram_gb! GB
echo.
:: Check current hibernation status
set "hib=UNKNOWN"
for /f "tokens=3" %%A in ('reg query "HKLM\SYSTEM\CurrentControlSet\Control\Power" /v HibernateEnabled 2^>nul ^| findstr /i "HibernateEnabled"') do (
if "%%A" == "0x1" (set "hib=ENABLED") else (set "hib=DISABLED")
)
echo Hibernation: !hib!
echo.
echo Options:
echo [1] Set to FULL (~75%% of RAM^)
echo [2] Set to REDUCED (~40%% of RAM^)
echo [3] Set CUSTOM percentage
echo [4] Disable hibernation entirely
echo [0] Exit
echo.
set /p "opt=Select: "
if "!opt!" == "1" goto do_full
if "!opt!" == "2" goto do_reduced
if "!opt!" == "3" goto do_custom
if "!opt!" == "4" goto do_disable
if "!opt!" == "0" exit /b
goto menu
:do_full
powercfg /h on >nul 2>&1
powercfg /h /type full
echo [OK] Set to full.
pause
goto menu
:do_reduced
powercfg /h on >nul 2>&1
powercfg /h /type reduced
echo [OK] Set to reduced.
pause
goto menu
:do_custom
set "pct=50"
set /p "pct=Enter percentage (40-100) [default: 50]: "
if !pct! lss 40 set "pct=40"
if !pct! gtr 100 set "pct=100"
powercfg /h on >nul 2>&1
powercfg /h /size !pct!
echo [OK] Set to !pct!%%.
pause
goto menu
:do_disable
powercfg /h off
echo [OK] Hibernation disabled. hiberfil.sys deleted.
pause
goto menu
Verifying the Current File Size
After making changes, you can verify the actual disk footprint of hiberfil.sys:
@echo off
setlocal enabledelayedexpansion
echo Checking hiberfil.sys size...
:: Use dir with hidden+system attributes
for /f "tokens=3" %%A in ('dir C:\hiberfil.sys /a:hs 2^>nul ^| findstr /i "hiberfil"') do (
set "size_raw=%%A"
)
if defined size_raw (
echo hiberfil.sys size: !size_raw! bytes
) else (
echo hiberfil.sys not found (hibernation is disabled^).
)
pause
Common Mistakes
The Wrong Way: Setting Below the Minimum
:: WRONG - 20% is below the 40% minimum
powercfg /h /size 20
Output Concern: The command will fail with an error stating that the percentage is below the acceptable range. The absolute minimum is 40%.
The Wrong Way: Setting /size Without Enabling Hibernation First
:: WRONG - If hibernation is off, /size has nothing to configure
powercfg /h /size 60
Output Concern:
If powercfg /h off was previously executed, the /size command fails or is ignored because there is no hibernation file to resize. Always run powercfg /h on before adjusting the size.
The Wrong Way: Expecting Full Hibernate at 40%
:: PROBLEMATIC - 40% may not be enough for a full hibernate
powercfg /h /size 40
shutdown /h
If physical RAM usage exceeds 40% at the time of hibernation, the system cannot fit the memory dump into the hibernation file. The hibernate command will fail with an error. For reliable full hibernation, use at least 75%.
Best Practices
- Enable hibernation before resizing: Always run
powercfg /h onbefore any/typeor/sizecommands. - Use
reducedfor desktop PCs: Desktop computers rarely use full hibernation. Setting the type toreducedsaves significant disk space while keeping Fast Startup functional. - Use
fullfor laptops: Laptop users who close the lid and expect the machine to resume later need the full hibernation file. - Monitor disk space: On machines with small SSDs (128 GB or 256 GB), the hibernation file can consume a meaningful percentage of total storage. Always account for it in disk capacity planning.
Conclusion
Setting the hibernate file size in Batch Script is managed through the powercfg /h /type and powercfg /h /size commands. By choosing between the full and reduced file types, or specifying a custom percentage, administrators can precisely balance disk space consumption against power management capabilities. Understanding the 40% minimum floor and the relationship between file size and hibernation reliability ensures that deployment scripts configure machines correctly for their intended use case.