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

Suzana Eree 811 Reputation points
2021-05-22T16:35:43.293+00:00

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}

OR

$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,322 questions
0 comments No comments
{count} vote

Accepted answer
  1. Suzana Eree 811 Reputation points
    2021-05-23T15:20:45.067+00:00

    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

    98892-image.png

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

    https://community.notepad-plus-plus.org/topic/21200/change-save-encoding-how-to-convert-800-txt-files-utf-8-to-utf-8-bom

    0 comments No comments

5 additional answers

Sort by: Most helpful
  1. Andreas Baumgarten 94,196 Reputation points MVP
    2021-05-22T19:07:35.517+00:00

    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)

    Regards
    Andreas Baumgarten

    3 people found this answer helpful.

  2. Suzana Eree 811 Reputation points
    2021-05-22T20:46:22.737+00:00

    hello sir @Andreas Baumgarten

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

    lB73Pp.jpg

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


  3. Andreas Baumgarten 94,196 Reputation points MVP
    2021-05-22T21:43:12.97+00:00

    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.

    98769-image.png

    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.
    https://learn.microsoft.com/en-us/answers/questions/396128/powershell-copy-a-stringword-from-a-bottom-line-to.html


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

    Regards
    Andreas Baumgarten


  4. Suzana Eree 811 Reputation points
    2021-05-23T05:18:11.03+00:00

    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\

    1621713973096-fb783d27-a840-47fa-96a5-3fcd432691fe-image.png