How to Set Default UTF-8 encoding for New Notepad Documents When Saving File

Part 1: Set Default UTF-8 encoding for New Notepad Documents When Saving File for Legacy Windows (Less than Version 1903)
# Create a file in UTF-8 encoding by using the New-Item command
# New-Item -Name TXTUTF-8.txt -ItemType File # Doesn't work on Windows 2016
# Borrowing the dot-net method https:// docs.microsoft.com/en-us/dotnet/api/system.text.utf8encoding?redirectedfrom=MSDN&view=net-5.0
$utf8Encoding = New-Object System.Text.UTF8Encoding $True
[System.IO.File]::WriteAllLines("$([System.IO.Path]::GetTempPath())TXTUTF-8.txt",'',$utf8Encoding)
Move-Item "$([System.IO.Path]::GetTempPath())TXTUTF-8.txt" "$ENV:WINDIR\SHELLNEW" -Force

# Creating registry key
$registryShellNew='REGISTRY::HKEY_CLASSES_ROOT\.txt\ShellNew'
$registryFileName='FileName'
$registryFileNameValue='TXTUTF-8.txt'
Set-ItemProperty -Path $registryShellNew -Name $registryFileName -Value $registryFileNameValue

# Reverse the change
Remove-ItemProperty -Path $registryShellNew -Name $registryFileName
Remove-Item "$ENV:WINDIR\SHELLNEW\TXTUTF-8.txt" -Force
Part 2: Set Notepad Default encoding UTF8 for Windows 10 (Version 1903 or Newer)
# Setting default encoding
$registryNotepad='REGISTRY::HKEY_CURRENT_USER\Software\Microsoft\Notepad'
$registryNotepadDefaultEncoding='iDefaultEncoding'
$registryNotepadDefaultEncodingValue=5
Set-ItemProperty -Path $registryNotepad -Name $registryNotepadDefaultEncoding -Value $registryNotepadDefaultEncodingValue

gi 'REGISTRY::HKEY_CURRENT_USER\Software\Microsoft\Notepad'

# The PoSH commands above recreates this registry value:

Windows Registry Editor Version 5.00

[HKEY_CURRENT_USER\Software\Microsoft\Notepad]
"iDefaultEncoding"=dword:00000001

; 1 = ANSI
; 2 = UTF-16 LE
; 3 = UTF-16 BE
; 4 = UTF-8 BOM
; 5 = UTF-8

# Reverse the change
Remove-ItemProperty -Path $registryNotepad -Name $registryNotepadDefaultEncoding

2 thoughts on “How to Set Default UTF-8 encoding for New Notepad Documents When Saving File”

  1. You seem to have your labeling wrong: “Part 1” seems to apply for “for Legacy Windows (Less than Version 1903)” and “Part 2” for the opposite (Windows 10 Version 1903 or later).

    Also for “Part 1”, I suggest you use “$Env:windir\ShellNew\” in place of “C:\Windows\SHELLNEW” and “[System.IO.Path]::GetTempPath()” in place of “C:\”. It might be nice to test for the existence of the registry key as well. I have not actually tested this but I envision something like the following (inspired by https:// web.archive.org/web/20201124012523/https:// gallery.technet.microsoft.com/scriptcenter/How-to-set-default-2d9669ae):

    $targetPath = "$Env:windir\ShellNew"
    $tempPath = [System.IO.Path]::GetTempPath()
    $templateFileName = 'TXTUTF-8.txt'
    $targetFile = Join-Path $targetPath $templateFileName
    $tempFile = Join-Path $tempPath $templateFileName

    $registryPathShellNew = "REGISTRY::HKEY_CLASSES_ROOT\.txt\ShellNew"
    $registryNameFileName = "FileName"

    Function Set-NotePadUTF8Default
    {
    $utf8Encoding = New-Object System.Text.UTF8Encoding $True
    [System.IO.File]::WriteAllLines($tempFile, '', $utf8Encoding)
    Move-Item $tempFile $targetPath -Force

    $registryItemShellNew = Get-Item -Path $registryPathShellNew
    If (-not ($registryItemShellNew.Property.Contains($registryNameFileName))) {
    New-ItemProperty -Path $registryPathShellNew -Name $registryNameFileName -Value $templateFileName
    } else {
    Set-ItemProperty -Path $registryPathShellNew -Name $registryNameFileName -Value $templateFileName
    }
    }

    Function Reset-NotePadUTF8Default
    {
    $registryItemShellNew = Get-Item -Path $registryPathShellNew
    If (-not ($registryItemShellNew.Property.Contains($registryNameFileName))) {
    } else {
    $registryValueFileName = Get-ItemProperty -Path $registryPathShellNew -Name $registryNameFileName
    Remove-ItemProperty -Path $registryPathShellNew -Name $registryNameFileName
    Remove-Item (Join-Path $targetPath $registryValueFileName) -Force
    }
    }

    Set-NotePadUTF8Default

Leave a Reply

Your email address will not be published. Required fields are marked *