How to set default app per filetype programmatically?

1

I want to get independent of installed apps and share most of my apps as portable versions in a cloud drive. So my idea is whenever I sit on a new device I just sync my cloud drive and start some kind of process which installs my setup.

By installing I mean setting up my windows settings and so on. And I want to set the default apps per filetype. For example bind .txt files to d:\cloud\notepad++\notepad++.exe

Here is a simplified script how I try to do this. I stripped ErrorActions and so on to make it more readable.

$exe = "d:\cloud\notepad++\notepad++.exe"
$app = "notepad++.exe"
$ext = ".txt"

# Remove old associations
Remove-Key -Key "HKCU:\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\$ext"
Remove-Key -Key "HKCR:\$ext"
Remove-Key -Key "HKCR:\$app"

# Setup new associations
cmd /c assoc $ext=$app
cmd /c ftype $app="$exe" "%1" "%*"
Set-ItemProperty -Path "HKCR:\$app" `
                    -Name "(Default)" `
                    -Value "Text file" `
                    -Type String  

This kind of works. Whenever I try to open a .txt file now it asks me which app to use and pre-selects notepad++.
Okay that's kind of annoying but I could live with that. Although. When I check always use this app to open .txt files it gets simply ignored. I always get this dialog.

enter image description here

Brettetete

Posted 2019-02-10T15:59:47.663

Reputation: 2 057

No answers