Script to change the file type association for PDF files on Windows 10

First, get the result of this command:

assoc .pdf

It gives the current association between .pdf and its document type. In my case, the output is:

FoxitPhantomPDF.Document

Based on this output, I created two .bat files:

ftype FoxitPhantomPDF.Document="C:\Program Files\SumatraPDF\SumatraPDF.exe" "%1"

and:

ftype FoxitPhantomPDF.Document="C:\Program Files (x86)\Foxit Software\Foxit PhantomPDF\FoxitPhantomPDF.exe" "%1"

You have to run the .bat files as an admin.

Sorry, but there is a problem there:

  • when I open an administrator prompt in Windows and type the above commands they work fine (!), but when I right click on the bat files and run them as administrator they don't work!

Unfortunately above answers are obsoleted and don't work anymore with Windows 10 18xx or 19xx. What I did find to work as admin or non-admin user was "SetUserFTA.exe" as per instructions below:

  1. Download "SetUserFTA.exe" and put it in a folder, example "D:\tools\SetDefaults\".

    Direct link: http://kolbi.cz/SetUserFTA.zip

    Source: http://kolbi.cz/blog/2017/10/25/setuserfta-userchoice-hash-defeated-set-file-type-associations-per-user/

  2. In the same folder ("D:\tools\SetDefaults\") create a text file named "my_config.txt" that contains needed associations, in this example: pdf to Okular and txt to Notepad++:

.pdf, applications\okular.exe
.txt, applications\notepad++.exe
  1. In the same folder ("D:\tools\SetDefaults\") create a bat file "SetDefaults.bat" that contains:
@echo off
D:
cd D:\tools\SetDefaults\
echo %DATE%-%TIME%: SetuserFTA is running >> LogFile.txt
SetuserFTA.exe  my_config.txt >> LogFile.txt
echo %DATE%-%TIME%: SetuserFTA is finnished. Restarting explorer... >> LogFile.txt
taskkill /F /IM explorer.exe
start explorer.exe
::pause
  1. Setup the "SetDefaults.bat" script to be called at logon in one of the following 3 ways:

    4a. if you have administrator rights, copy the bat file into C:\WINDOWS\System32\GroupPolicy\User\Scripts\Logon

    4b. or, also if you have administrator rights, from start menu type and open gpedit.msc -> Local Computer Policy -> User Configuration -> Windows Settings -> Scripts (Logon/Logoff) -> Logon -> select Add then set: Script Name = D:\tools\SetDefaults\SetDefaults.bat Script Parameters = leave it empty

    4c. or if you don't have administrator rights, go to "C:\Users\Your.Name\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup\" and create a shortcut "SetDefaults.lnk" pointing to "D:\tools\SetDefaults\SetDefaults.bat"

Note: explorer killing and restarting is not needed for 4a and 4b (if you have administrator rights) as from there the script is called much earlier. However for 4c it is needed as the script is called around 10s after explorer is already running.

For debugging check D:\tools\SetDefaults\LogFile.txt which should have a content like this:

11/06/2020-14:06:34.03: SetuserFTA is running 
11/06/2020-14:06:34.06: SetuserFTA is finnished. Restarting explorer... 

Note: there is an open source (in Power Basic) alternative which I didn't tried: https://danysys.com/set-file-type-association-default-application-command-line-windows-10-userchoice-hash-internal-method/

Optional: to set also the default browser, download "SetDefaultBrowser.exe" and put in in the same folder.

Direct link: http://kolbi.cz/SetDefaultBrowser.zip

Source: http://kolbi.cz/blog/2017/11/10/setdefaultbrowser-set-the-default-browser-per-user-on-windows-10-and-server-2016-build-1607/

Then enhance the above "SetDefaults.bat" as follows:

@echo off
D:
cd D:\tools\totalcmd\Tools\SysBackupBcdBootUSBFormatPart\SetDefaults\
taskkill /F /IM explorer.exe
echo %DATE%-%TIME%: Killing explorer. Run SetuserFTA >> LogFile.txt
SetuserFTA.exe  my_config.txt >> LogFile.txt
echo %DATE%-%TIME%: SetuserFTA is finnished. Restarting explorer... >> LogFile.txt
start explorer.exe
timeout /T 2
SetDefaultBrowser.exe chrome  >> LogFile.txt
echo %DATE%-%TIME%: SetDefaultBrowser is finnished. >> LogFile.txt
::pause

Note, that browser setting has to be done after explorer was restarted, this is why the 2s delay...