Skip to main content

How to Set the Default App for a File Type in Batch Script

Setting default applications for specific file extensions is a common requirement in managed IT environments, enterprise deployments, and personal workstation setups. For instance, you might want to force all .pdf files to open in a specific lightweight viewer instead of the browser, or map .py files to your favorite code editor.

In this guide, we will explore how to assign default apps to file types using Batch Script. Historically, Windows handled defaults through simple registry entries or the assoc and ftype commands, but modern Windows 10 and 11 architectures have fundamentally changed this approach to prevent malicious hijacking.

The Historic Approach: ASSOC and FTYPE (Pre-Windows 10)

For legacy systems (Windows 7 and earlier), or for obscure, unregistered file types, the built-in assoc and ftype commands were the standard.

Step 1: Define the File Type Command (FTYPE)

ftype associates a programmatic file type name with an executable command string.

@echo off
setlocal

:: Requires Administrator privileges
net session >nul 2>&1
if %errorlevel% neq 0 (
echo [ERROR] Must run as Administrator to modify file type associations.
pause
exit /b 1
)

:: Define 'MyTextApp' to open with Notepad++
ftype MyTextApp="C:\Program Files\Notepad++\notepad++.exe" "%%1"

echo [OK] FTYPE registered.
pause

Step 2: Bind the Extension (ASSOC)

assoc links a file extension (e.g., .log) to the programmatic name defined in ftype.

@echo off
setlocal

:: Requires Administrator privileges
net session >nul 2>&1
if %errorlevel% neq 0 (
echo [ERROR] Must run as Administrator to modify file associations.
pause
exit /b 1
)

:: Link .log files to MyTextApp
assoc .log=MyTextApp

echo [OK] ASSOC registered.
pause

Why ASSOC and FTYPE Fail on Modern Windows

Starting with Windows 10, Microsoft introduced the UserChoice Hash security mechanism. If you use assoc to change a common file type (like .pdf or .html), Windows 10/11 will detect the manual tampering, display a notification ("An app default was reset"), and immediately revert the association back to Edge or the Windows default.

Because Batch cannot generate the cryptographic UserChoice Hash, the assoc command is effectively obsolete for common file types on modern systems.

The Modern Approach: Default Associations XML (Windows 10/11)

To programmatically set default apps on modern Windows without triggering security resets, you must use Microsoft's official Deployment Image Servicing and Management (DISM) tool or Group Policy.

This process involves establishing your desired defaults on a reference machine, exporting an XML file, and importing that XML via Batch Script on target machines.

Step 1: Export Defaults from a Reference Machine

First, on a test machine, manually set all your desired default apps via the Windows Settings GUI (Settings > Apps > Default apps).

Then, open an Administrator Command Prompt and export those settings:

Dism /Online /Export-DefaultAppAssociations:"C:\Temp\AppDefaults.xml"

This generates an XML file looking roughly like this:

<?xml version="1.0" encoding="UTF-8"?>
<DefaultAssociations>
<Association Identifier=".htm" ProgId="FirefoxHTML" ApplicationName="Firefox" />
<Association Identifier=".html" ProgId="FirefoxHTML" ApplicationName="Firefox" />
<Association Identifier=".pdf" ProgId="SumatraPDF" ApplicationName="SumatraPDF" />
<Association Identifier=".txt" ProgId="Applications\notepad++.exe" ApplicationName="Notepad++" />
</DefaultAssociations>

Step 2: Pruning the XML

The exported file contains every association on the system. It is best practice to edit the XML in a text editor and remove everything except the specific extensions you want to manage. This prevents your script from accidentally overwriting user preferences for unrelated file types.

Save your pruned XML file (e.g., CustomDefaults.xml) alongside your Batch script.

Step 3: Import Defaults via Batch Script using DISM

Now, you can write a Batch Script that deploys this XML file to new user profiles systematically.

@echo off
setlocal

:: Require Admin privileges for DISM
net session >nul 2>&1
if %errorlevel% neq 0 (
echo [ERROR] Must run as Administrator to set default apps.
pause
exit /b 1
)

set "xml_file=%~dp0CustomDefaults.xml"

if not exist "%xml_file%" (
echo [ERROR] XML file "%xml_file%" not found.
pause
exit /b 1
)

echo Importing default app associations...
Dism /Online /Import-DefaultAppAssociations:"%xml_file%"

if %errorlevel% == 0 (
echo [OK] Default apps applied successfully.
) else (
echo [ERROR] Failed to import associations.
)

pause
warning

Important Limitation: DISM /Import-DefaultAppAssociations only applies the settings to new user profiles created after the script is run. It does not immediately override the settings for the currently logged-in user. This makes it ideal for machine imaging and provisioning, but less useful for changing active user environments on the fly.

Group Policy Implementation (Enterprise)

If you need to change default apps for existing users actively, running Batch Scripts is not the recommended path. Instead, you should configure the "Set a default associations configuration file" Group Policy Object using the XML file you exported in Step 1.

Path in gpedit.msc: Computer Configuration > Administrative Templates > Windows Components > File Explorer > Set a default associations configuration file

Third-Party Tools: SetUserFTA (Advanced)

Because the native XML import only affects new profiles, some IT professionals use third-party reverse-engineered tools like SetUserFTA.exe. This tool calculates the necessary UserChoice Hash and writes directly to the registry, allowing on-the-fly changes for active users.

If you have SetUserFTA.exe in your directory, your Batch Script becomes trivially simple:

@echo off
:: Does not require admin rights; works instantly for the current user

echo Setting .pdf default to SumatraPDF...
SetUserFTA.exe .pdf SumatraPDF

echo Setting .html default to Chrome...
SetUserFTA.exe .html ChromeHTML

echo [OK] Live associations updated.
pause
info

Tools like SetUserFTA rely on undocumented Windows hashing algorithms. While highly effective, they must be updated occasionally when Microsoft changes the hash calculation in major OS builds.

Common Mistakes

The Wrong Way: Direct Registry Manipulation on Windows 10+

:: WRONG - Triggers the "App Default Reset" warning
REG ADD "HKCU\Software\Classes\.pdf" /ve /d "MyPDFReader" /f

Output Concern: While this worked on Windows 7, Windows 10/11 actively monitors these registry keys. Without the corresponding cryptographic hash in the UserChoice subkey, Windows assumes malware modified the registry, revokes the change instantly, and resets .pdf back to Microsoft Edge.

The Correct Way: Use Export/Import XML or Group Policy

Always use the supported DISM XML approach for imaging, or rely on specialized tools designed to generate the correct security hashes if live modification is required.

Conclusion

Setting default applications for file types via Batch Script requires understanding the modern Windows security architecture. The legacy assoc and ftype commands are largely retired for common extensions due to anti-hijacking protections. Today, the most reliable and architecturally sound method is exporting desired associations to an XML file using DISM, and deploying that XML via a Batch Script during machine provisioning. For instant, active-user modifications without Group Policy, specialized third-party hashing tools are required to bypass modern registry protections safely.