I am trying to find a way to open a non-txt file (in this case, the hosts file) in the default text editor using PowerShell.
I made some progress after seeing this Reddit post, but the $txt_editor
result always returns Notepad.exe, even though Notepad++ is my default editor for txt files.
$hosts_file = "$env:windir\System32\drivers\etc\hosts"
$txt_editor = ((Get-ItemProperty -Path 'Registry::HKEY_CLASSES_ROOT\txtfile\shell\open\command').'(Default)').trimend(" %1")
Start-Process -FilePath $txt_editor -Verb Runas -ArgumentList $hosts_file
This also returns Notepad.exe:
(Get-ItemProperty -Path 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.txt\OpenWithList' -Name a).a
If I look at the above location in Registry Editor, I do see Notepad++ listed with the key d
, but I don't know how to tell from only looking at a Registry key what the default text editor is, because the two solutions I saw in Reddit do not work.
I am using Windows 10, and the solution I am looking for will return the actual default text editor file location, so that it can be used to open a file as shown above.
This question still has not received an answer to my actual question, since the only answer doesn't show how to open a non-txt file using the configured default text file editor.
Update: I'm now trying to replicate this on Windows 11, and the registry key HKEY_CLASSES_ROOT\txtfile\shell\open\command
doesn't exist. I need a solution that will include a solution for this aspect as well, if the registry is involved. I configured Notepad++ as the default, and a key was created at HKEY_CLASSES_ROOT\txtfilelegacy\shell\printto\command
.
notepad++
allows such thing (notepad.exe
allows to pass a path as argument), you can simply do:& (Get-ItemProperty -Path 'HKCU:\.....' -Name a).a C:\Windows\System32\drivers\etc\hosts
– Litalitany.txt
? – Name