Search code examples
powershellregistry

PowerShell: opening a file in default txt editor


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.


Solution

  • Using the information in @FoxDeploy's answer, I was able to construct a working PowerShell function.

    function Edit-HostsFile {
        # Self-elevate if required
        if (-Not ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] 'Administrator')) {
            if ([int](Get-CimInstance -Class Win32_OperatingSystem | Select-Object -ExpandProperty BuildNumber) -ge 6000) {
                $hosts_file = "$env:windir\System32\drivers\etc\hosts"
                # Get the configured application default for editing .txt files.
                $txt_app = ((Get-ItemProperty -Path Registry::HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.txt\UserChoice -Name ProgId).ProgId)
                $txt_editor = ((Get-ItemProperty -Path Registry::HKEY_CLASSES_ROOT\$($txt_app)\shell\open\command).'(Default)').replace(' "%1"', '')
                # Open the hosts file in the editor.
                Start-Process -FilePath $txt_editor -Verb Runas -ArgumentList $hosts_file
            }
        }
    }
    

    I hadn't gotten a working solution using the information in the answer when I tried a few years ago, but I was able to figure it out after trying again. A more complete solution with code would have been helpful, but it didn't take too long to create.