Console Interface

UltraDefrag command line (console) interface can be used to automate defragmentation via batch scripts and scheduled tasks. Also it can be used instead of the graphical interface by those who prefer command line environment.

Also UltraDefrag uses its command line interface to handle its context menu entries in Windows Explorer. Right click any object and select Defragment with UltraDefrag to rejoin its clusters. To stop defragmentation hit Ctrl+C or close the command prompt. To configure the context menu handler click Settings > Options in UltraDefrag GUI or edit the following file manually: {installation folder}\conf\options.lua.

The context menu handler is not available from the portable edition of the program.

UltraDefrag puts its command line tool to the %SystemRoot%\system32 folder, so it can be used without the full path specification. Just make sure you run it from an elevated command prompt as it requires administrative rights.


udefrag [command] [options] [driveletter:] [path(s)]


-a, --analyze
Analyze specified objects.
-o, --optimize
Perform full optimization.
-q, --quick-optimization
Perform quick optimization.
Optimize master file tables only.
-l, --list-available-volumes
List all fixed disks available for defragmentation.
-la, --list-available-volumes=all
List all available disks, including removable.
-h, -?, --help
Display help.
The commands are exclusive and can't be combined with each other. If none is specified the program defragments selected objects.


-b, --use-system-color-scheme
Disable colorization of output.
-p, --suppress-progress-indicator
Hide the progress indicator and the cluster map.
-v, --show-volume-information
Show disk information after the job completion.
-m, --show-cluster-map
Show the cluster map.
Set color of the cluster map border. Available colors: black, white, red, green, blue, yellow, magenta, cyan, darkred, darkgreen, darkblue, darkyellow, darkmagenta, darkcyan, gray. Yellow is used by default.
Set the character to draw the cluster map with. Type it directly or use its hexadecimal number (in range 0x1 ... 0xFF). For instance, use --map-symbol=0x1 to draw the map with a smile character. The % symbol is used by default.
Set height of the cluster map. The default value is 10.
Set width of the cluster map. The default value is 68.
Expand the cluster map to use the entire console window.
Wait for completion of other UltraDefrag instances before the job startup (useful for scheduled tasks).
List selected objects and display a prompt to hit any key after the job completion. This switch is intended to handle the context menu entries in Windows Explorer, so the program interpretes the options.lua file mentioned above and ignores all the environment variables listed below.

Drive letters:

List of space separated drive letters or one of the following switches:

Process all available drives.
Process all non-removable drives.


List of space separated paths which need to be defragmented. Both absolute and relative paths are supported, as well as wildcards: the question mark (?) matches any one character, while the asterisk (*) - any zero or more characters. Thus *.* matches any file with an extension while a single asterisk (*) matches anything (with or without an extension). All paths including spaces must be enclosed by double quotes:

:: defragment drives d: and e:
udefrag d: e:
:: defragment the contents of the USERPROFILE folder
udefrag "%USERPROFILE%\*"
:: defragment everything inside of the '..\Test Files' folder
udefrag "..\Test Files\*"
:: defragment some individual file
udefrag C:\WINDOWS\WindowsUpdate.log
:: defragment them all at once
udefrag d: e: "%USERPROFILE%\*" "..\Test Files\*" C:\WINDOWS\WindowsUpdate.log
:: rejoin clusters of the WINDOWS folder
:: (its contents will be left untouched)
udefrag C:\WINDOWS

If command line is empty the program displays help.

Environment variables

Filter variables

List of semicolon separated paths which need to be defragmented.
Empty list means that everything needs to be defragmented.
List of semicolon separated paths which need to be skipped, i.e. left untouched.
Eliminate only fragments smaller than specified.
The following size suffixes are accepted: KB, MB, GB, TB, PB, EB.
Exclude all files larger than specified.
The following size suffixes are accepted: KB, MB, GB, TB, PB, EB.
For optimization only, exclude all files larger than specified.
The following size suffixes are accepted: KB, MB, GB, TB, PB, EB.
The default value is 20MB.
Exclude files having less fragments than specified.
Set sorting criteria for the disk optimization. PATH is used by default, it forces to sort files by their paths. Four more options are available: SIZE (sort by size), C_TIME (sort by creation time), M_TIME (sort by last modification time) and A_TIME (sort by last access time).
Set sorting order for the disk optimization. ASC (ascending) is used by default. DESC (descending) forces to sort files in reverse order.
Cancel all tasks except of the MFT optimization when the disk fragmentation level is below than specified.

Process variables

Terminate the job automatically when the specified time interval elapses. The following time format is accepted: Ay Bd Ch Dm Es. Here A,B,C,D,E represent integer numbers while y,d,h,m,s represent years, days, hours, minutes and seconds.
The progress refresh interval, in milliseconds. The default value is 100.
Set it to 1 (one) to disable generation of the file fragmentation reports.

Debugging variables

Set amount of debugging output. NORMAL is used by default, DETAILED can be used to collect information for a bug report, PARANOID turns on really huge amount of debugging information.
Set log file path (including file name) to redirect debugging output to a file.
Set it to 1 (one) to avoid physical movements of files, i.e. to simulate the disk processing. This allows to check out algorithms quickly.


Batch scripts can be used to automate complex defragmentation tasks:

@echo off
:: save debugging information for troubleshooting
set UD_LOG_FILE_PATH=C:\Windows\Temp\udefrag.log
:: terminate the processing after 6 hours and 30 minutes
set UD_TIME_LIMIT=6h 30m
:: exclude temporary stuff
set UD_EX_FILTER=*system volume information*;*temp*;*tmp*;*recycle*
:: exclude archives as well
set UD_EX_FILTER=%UD_EX_FILTER%;*.zip;*.7z;*.rar
:: eliminate only fragments below 20 MB in size
:: list all drives available for defragmentation
udefrag -la
:: check all available drives for errors
for /F "tokens=1 skip=8" %%D in ('udefrag -la') do chkdsk %%D
:: analyze all fixed drives
udefrag -a --all-fixed
:: defragment everything on drive c:
udefrag c:
:: defragment small audio files on drive d:
set UD_IN_FILTER=*.mp3
udefrag "D:\My Docs\Music\*"
:: defragment large video files having at least 10 fragments
set UD_IN_FILTER=*.avi;*.mkv
udefrag "D:\My Docs\Movies\*"
:: optimize master file tables on drives c: and d:
udefrag --optimize-mft c: d:
:: reset filters used above
:: sort files on drive g: by creation time in descending order
udefrag -o g:
:: defragment drive h: only if its fragmentation level is above 10%
udefrag h:
:: defragment all fixed disks except of c:, d:, g: and h:
set UD_EX_FILTER=%UD_EX_FILTER%;C:\*;D:\*;G:\*;H:\*
udefrag --all-fixed
:: defragment a disk with no drive letter assigned,
:: mounted to C:\Games, using the diskpart utility
:: first of all, assign a drive letter to the disk
echo select volume c:\games > script
echo assign letter=t >> script
diskpart /s script
:: then, defragment the disk
udefrag t:
:: after all, remove the assigned letter
echo select volume c:\games > script
echo remove letter=t >> script
diskpart /s script
:: hibernate the computer using a special
:: command line tool shipped with UltraDefrag
hibernate4win now
:: shut the computer down if hibernation failed
if errorlevel 1 shutdown -s -t 00