How to set default encoding to UTF-8 for notepad

Introduction

This sample demonstrates how to set default encoding of notepad to UTF-8.

 

For the VBScript version, please visit https://gallery.technet.microsoft.com/How-to-set-default-d4f20aba

Scenarios

The Windows notepad program sets ANSI encoding as default. Very often, when non-English user types a lot of Unicode words and save it, the notepad will pop up an alert to ask for saving as Unicode. If user click no accidently, all his input will be lost except ANSI characters.

 

This script can help users set the default notepad encoding to UTF-8.

Prerequisites

• PowerShell 2.0 or above versions

Script

• Open and decompress the archive of the sample

• Run the PowerShell as administrator at the folder containing the script and a txt file

• Run the command “Set-ExecutionPolicy -ExecutionPolicy RemoteSigned” and type “A” at prompt

• Run the script “SetDefaultUTF8Encoding.ps1” directly

• To check the result, you can create a txt file by right clicking the desktop and then clicking save as, so that you will find the encoding automatically set as UTF8.

 

Here are the code snippets for your reference

 

PowerShell
Edit|Remove
$RegistryPrefix = "Registry" 
$NotePadKeyPath = "$RegistryPrefix::HKEY_CLASSES_ROOT\.txt\ShellNew" 
$FileNameKey = "FileName" 
$NotePadTemplatePath = "TXTUTF-8.txt" 
 
Function Set-NotePadUTF8Default 
{ 
    $NotpadRegistryItem = Get-Item -Path $NotePadKeyPath 
 
    If (-not ($NotpadRegistryItem.Property.Contains($FileNameKey))) { 
        New-ItemProperty -Path $NotePadKeyPath -Name $FileNameKey -Value $NotePadTemplatePath 
    } else { 
        Set-ItemProperty -Path $NotePadKeyPath -Name $FileNameKey -Value $NotePadTemplatePath 
    } 
    Copy-Item -Destination "$Env:windir\ShellNew\" -Path $NotePadTemplatePath -Force 
} 

 

Additional Resources

• MS community: https://answers.microsoft.com/en-us/windows/forum/windows_7-windows_programs/default-utf-8-encoding-for-new-notepad-documents/525f0ae7-121e-4eac-a6c2-cfe6b498712c

 

Microsoft All-In-One Script Framework is an automation script sample library for IT Professionals. The key value that All-In-One Script Framework is trying to deliver is Scenario-Focused Script Samples driven by IT Pros' real-world pains and needs. The team is monitoring all TechNet forums, IT Pros' support calls to Microsoft, and script requests submitted to TechNet Script Repository. We collect frequently asked IT scenarios, and create script samples to automate the tasks and save some time for IT Pros. The team of All-In-One Script Framework sincerely hope that these customer-driven automation script samples can help our IT community in this script-centric move.