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,555 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. Rich Matheisen 46,816 Reputation points
    2022-08-09T19:51:02.16+00:00

    I know this is a late answer to this question. I meant to answer it much earlier, but I lost track of the topic.

    PowerShell 5.1, by itself, can't accomplish this but it's easily done using a few lines of code using the .Net System.IO.StreamWriter and the System.Text.UTF8Encoding classes.

    Here an example:

    $in = Get-Content -Path C:\junk\123a.txt -Raw       # get the whole file in one gulp  
      
    $Utf8NoBomEncoding = New-Object System.Text.UTF8Encoding $False  
    $sw = [System.IO.StreamWriter]::new("c:\Junk\New123a.txt", $Utf8NoBomEncoding)  # No BOM!  
    $sw.Write($in)  
    $sw.Flush()  
    $sw.Close()  
    

    The input file looks like this (note the 0xFFFE at the beginning):
    229721-123atxt.jpg

    The Output file looks like this (the file begins with the 1st byte of data -- the file wasn't opened in a way that would clobber an existing file, so the data's there twice; once for each time I ran the script):
    229646-new123a.jpg

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.