Would like to assign certain filetypes to a software by script (preferably by PowerShell or .reg) in Windows 11.
This PowerShell-Code worked fine over one year ago:
cmd /c "assoc .rhistory = rhistory_file"
cmd /c "ftype rhistory_file = "`"C:\Program Files\RStudio\rstudio.exe`" `"%1`"""
Tried also something like this:
$RStudioPath = "C:\Program Files\RStudio\rstudio.exe"
$FileType = ".rhistory"
$FileAssociation = [Microsoft.Win32.Registry]::ClassesRoot.CreateSubKey($FileType)
$FileAssociation.SetValue("", "RStudio.rhistory")
$FileAssociation.CreateSubKey("shell")
$Command = $FileAssociation.CreateSubKey("shell\open\command")
$Command.SetValue("", "`"$RStudioPath`" `"%1`"")
Can somebody help? Thank you.
Assuming that you've run them with elevation, i.e. as administrator, your commands look correct in principle.
However, on a per-filename-extension basis, user-specific overrides can be in place on a given machine, via File Explorer, namely:
If the current user has interactively chosen to always open a file with a given extension with a specific program, by right-clicking on such a file in File Explorer, selecting
Open with > Choose another app, selecting the app of interest and then clicking onAlwaysThrough a mechanism that is unclear to me, some applications - notably Visual Studio Code - manage to install such a persistent override even if you click on
Just onceor even just drag a file to an open Visual Studio Code windoe.Either way, in order to undo such an override, you'll need to remove it from the registry at
HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\FileExts\<.extension>; in your case (using PowerShell'sHKCU:drive to refer to theHKEY_CURRENT_USERhive):The caveat is that you'll have to repeat the above whenever you (the current user) choses to open a file with the extension of interest with an application such as Visual Studio Code again - even with the intent of doing so ad hoc only.