Powershell: Change / Save encoding How to convert several txt files UTF-8 to UTF-8-BOM

Suzana Eree 786 Reputation points

Hello. I need to convert several txt files , located in C:\Folder1 from UTF-8 to UTF-8-BOM.

I had try these two variants, but non of them works:

get-item C:\Folder1*.* | foreach-object {get-content -Encoding utf8BOM $_ | out-file ("C:\Folder1" + $_.Name) -encoding default}


$MyPath = "C:\Folder1"
Get-ChildItem -Path $sourcedir -Filter *.txt | ForEach-Object {

# This variable can be reused
$utf8 = New-Object System.Text.UTF8Encoding $false

$MyFile = Get-Content $MyPath -Raw
Set-Content -Value $utf8.GetBytes($MyFile) -Encoding Byte -Path $MyPath

Does anyone know the solution?

Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,085 questions
0 comments No comments
{count} votes

Accepted answer
  1. Suzana Eree 786 Reputation points

    yes, the job is done. But if there is a solution also in Poweshell, It is welcome.

    also, you can find HERE another solutions for converting UTF-8 to UTF-8-BOM. Seems that you can do this job using notepad++ and REGEX, I test also this solution and WORKS !

    For example, in Notepad++, open the Find in Files dialog ( Ctrl + Shift + F ). And use Regular Expression:

    Search: \A

    Replace By \x{FEFF}

    FILTERS: *.html


    For other great solutions for converting UTF-8 in UTF-8-BOM check this link below:


    0 comments No comments

5 additional answers

Sort by: Most helpful
  1. Andreas Baumgarten 69,466 Reputation points MVP

    Hi @Suzana Eree ,

    Please try this:

    $a = "./Junk/testfile-utf8.txt"  
    $b = "./Junk/testfile-utf8-2bom.txt"  
    (Get-Content -path $a) | Set-Content -Encoding UTF8BOM -Path $b  


    (If the reply was helpful please don't forget to upvote and/or accept as answer, thank you)

    Andreas Baumgarten

    3 people found this answer helpful.
    0 comments No comments

  2. Suzana Eree 786 Reputation points

    hello sir @Andreas Baumgarten

    I test your code, I have an error. See this :


    Also, I must convert more than 600 txt files, not just one .txt file :(

  3. Andreas Baumgarten 69,466 Reputation points MVP

    Hi @Suzana Eree ,

    please try this:

    $a = "./Junk/testfile-utf8.txt"  
    $b = "./Junk/testfile-utf8-2bom.txt"  
    (Get-Content -path $a) | Set-Content -Encoding UTF8 -Path $b  

    Even with -Encoding UTF8 it creates an UTF8-BOM encoded file. At least Notepad++ is showing the Encoding UTF8-BOM for the file.


    Just take a look in your questions and answers of the past and you will find how to do this for each file in a folder.

    (If the reply was helpful please don't forget to upvote and/or accept as answer, thank you)

    Andreas Baumgarten

  4. Suzana Eree 786 Reputation points

    I find on internet a great and simple solution that works by using notepad++. You can use a Pyhon script:

    1. First install the plugin named "Python Script". Go to Menu -> Plugins -> Plugins Admin (select Python Script ). After install this plugin select New Script and save the code below. The run the script.

    You can find the Python code here: https://pastebin.com/KkZbqJWA

    After you run the code, you will get a pop up window that it prompts you thusly. Just write your Path over there, the location of your folder files, for example c:\Folder1\