Skip to main content

How to Reset File Type Associations to Default in Batch Script

Messed-up file associations can be incredibly frustrating. If a user accidentally sets .exe files to open with Notepad, or .zip files to open with a broken third-party tool, their system becomes nearly unusable. In this guide, we will explore how to use Batch Script to reset file type associations back to Windows defaults.

This technique is a critical component of helpdesk scripts, self-healing utilities, and system repair toolkits.

The Challenge on Modern Windows

Before Windows 10, resetting file associations was as simple as manipulating the HKEY_CLASSES_ROOT registry hive directly via REG DELETE or using the built-in assoc command.

However, Microsoft introduced a security feature called UserChoice Hash starting in Windows 10 to prevent malware from hijacking file types. This means that if a user manually selected an app via the "Open with..." dialog, Windows stores that choice securely in the UserChoice registry subkey. You cannot simply overwrite this key from a Batch Script without triggering an "App default was reset" error, and Windows will immediately revert your change.

Fortunately, there are still effective methods to reset associations, either by clearing the customized choices or by using built-in Windows repair tools.

Method 1: The Classic ASSOC Command (Windows 7 / Legacy Types)

For older versions of Windows, or for file types that haven't been secured by modern hashing mechanisms, the assoc command remains the standard method.

Resetting a Specific Extension

To reset an extension (e.g., .txt) so that Windows forgets any custom program associated with it:

@echo off
:: Requires Administrator privileges

echo Resetting .txt association...
assoc .txt=

echo [OK] .txt reset.
pause

Setting the extension equal to nothing (=) deletes the association. Windows will then fall back to its internal defaults the next time the user tries to open a .txt file.

Resetting Multiple Extensions

If a virus or rogue program hijacked a batch of extensions, you can clear them in a loop.

@echo off
setlocal
:: Requires Admin rights

set "extensions=.txt .log .ini .cfg"

for %%e in (%extensions%) do (
echo Clearing association for %%e
assoc %%e=
)

echo [OK] Selected extensions cleared.
pause

Method 2: Clearing the UserChoice Subkey (Windows 10/11)

When a user overrides a default app (e.g., choosing Chrome for .pdf), Windows writes that choice to the current user's registry under a specific UserChoice key. If you delete this key, Windows forgets the user's preference and reverts to the system default pattern (e.g., Microsoft Edge for .pdf).

This is the most effective way to "reset" associations in a modern environment without triggering the hash protection error.

Developing the Reset Script

The subkeys are located at: HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.[extension]\UserChoice

Deleting this key drops the user override.

@echo off
setlocal enabledelayedexpansion

:: Request the extension from the user
set "ext="
set /p "ext=Enter the extension to reset (e.g., .pdf): "

if not defined ext (
echo [ERROR] No extension provided.
pause
exit /b 1
)

:: Ensure the extension starts with a dot
if "!ext:~0,1!" neq "." set "ext=.!ext!"

set "reg_key=HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\!ext!\UserChoice"

echo Checking registry for custom override...

:: Check if the UserChoice key exists
reg query "!reg_key!" >nul 2>&1
if !errorlevel! equ 0 (
echo Override found. Proceeding with deletion...

:: Delete the key silently
reg delete "!reg_key!" /f >nul

if !errorlevel! equ 0 (
echo [OK] User preference for !ext! has been cleared.
echo Windows will now use the system default.
) else (
echo [ERROR] Failed to delete the key. It may be protected.
)
) else (
echo [INFO] No user override found for !ext!. It is already at system default.
)

pause

Explaining the Mechanism

By deleting the UserChoice key from HKCU (HKEY_CURRENT_USER), you are safely removing the "Open With..." selection. Since you are not writing a new value without a valid hash, Windows does not throw a security error. It simply notices the user has no preference and falls back to the master defaults governed by Microsoft or the XML provisioning package.

Method 3: Fixing Critical System Executables (.exe, .bat, .cmd)

If .exe or .bat file associations get corrupted, standard tools like Command Prompt or Registry Editor won't even launch properly. In this disaster scenario, you need to import a clean registry file that restores the fundamental HKEY_CLASSES_ROOT definitions for executables.

You can embed the clean registry entries inside your Batch Script. Wait, if .bat is broken, how do you run the script? Typically, you rename the script from repair.bat to repair.cmd (if .cmd still works), or run it via a shortcut.

The Emergency .exe and .bat Restorer

@echo off
echo Preparing to reset .EXE and .BAT associations...

:: Reset the core exe class
REG ADD "HKCR\exefile" /ve /d "Application" /f >nul
REG ADD "HKCR\exefile\shell\open\command" /ve /d "\"%%1\" %%*" /f >nul

:: Reset the core bat class
REG ADD "HKCR\batfile" /ve /d "Windows Batch File" /f >nul
REG ADD "HKCR\batfile\shell\open\command" /ve /d "\"%%1\" %%*" /f >nul

:: Restore the extension mappings
assoc .exe=exefile
assoc .bat=batfile

echo [OK] Executable associations restored.
pause

Returning the Entire Machine to Base Windows Defaults

If the system associations are totally ravaged, perhaps by a malware infection, you might want to return every single file type to a fresh, factory-state default.

Windows 10/11 includes a feature inside the Settings GUI to "Reset all default apps." However, automating this across a fleet of computers is difficult.

The most common approach in an enterprise environment is to export a known-good default associations XML file from a clean machine, and import it using the Deployment Image Servicing and Management (DISM) tool.

@echo off
:: Requires Admin rights
net session >nul 2>&1
if %errorlevel% neq 0 (
echo [ERROR] Must run as Admin.
pause
exit /b 1
)

set "xml_path=%~dp0CleanDefaults.xml"

if not exist "%xml_path%" (
echo [ERROR] Cannot find CleanDefaults.xml.
pause
exit /b 1
)

echo Importing clean associations XML via DISM...
Dism /Online /Import-DefaultAppAssociations:"%xml_path%"

if %errorlevel% equ 0 (
echo [OK] Default apps reset to XML baseline.
) else (
echo [ERROR] DISM import failed. Check the XML file format.
)
pause
warning

Using DISM /Import-DefaultAppAssociations only applies to new user profiles moving forward. To affect current users, you must use the UserChoice deletion technique shown in Method 2 on a per-extension basis.

Common Mistakes

The Wrong Way: Trying to Overwrite UserChoice without hashes

:: WRONG - Triggers Windows security protection
REG ADD "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.txt\UserChoice" /v ProgId /t REG_SZ /d "Notepad.exe" /f

Output Concern: Windows will immediately display a toast notification reading "An app default was reset" and automatically reverse your change before the user even clicks the file.

The Correct Way: Use REG DELETE

If you want to reset the file, just delete the UserChoice key via REG DELETE. Don't try to manually fake the association value.

Best Practices

  1. Clear User overrides first: Always start troubleshooting by deleting the user's custom preference in HKCU\...\FileExts\[ext]\UserChoice. This solves 90% of file association issues.
  2. Back up the registry: Before running aggressive .exe restorer scripts, confirm they contain the correct "%1" %* syntax. A typo here will instantly break the ability to launch any program on the computer.
  3. Combine DISM with Registry clears: For major deployments, use DISM to establish a solid baseline for the machine, and use REG DELETE in login scripts if a user's specific association breaks.

Conclusion

Resetting file type associations in Batch Script requires navigating around modern Windows security mechanisms designed to protect those very associations. While the old assoc command is adequate for legacy extensions, clearing the active user's UserChoice registry key is the safest and most effective strategy for resolving broken defaults in Windows 10/11 environments. For catastrophic failures affecting .exe and .bat execution, manually reprogramming the core HKEY_CLASSES_ROOT classes through REG ADD serves as a reliable last resort.