28

I need to convert a text file to UTF-8 format via Windows command prompt. This needs to be done on another machine and I do not have rights to install software on that machine. I need something like:

c:\notepad   source-file target-file --encoding option

Is there a Windows command prompt utility which can do it?

5 Answers 5

45

I need to convert a text file to utf-8 format via windows command prompt

You can easily do this with PowerShell:

Get-Content .\test.txt | Set-Content -Encoding utf8 test-utf8.txt

Further Reading

13
  • Additionally, you need to switch to powershell in order to execute the command and then exit it to go back to windows command prompt. Jan 5, 2017 at 16:09
  • 1
    @vibs2006 Use a ForEach loop. See ForEach - PowerShell - SS64.com.
    – DavidPostill
    Jan 28, 2019 at 18:32
  • 1
    Will this method convert to plain UTF-8 or UTF-8-BOM? Oct 23, 2019 at 22:28
  • 1
    @JustMe See my comment above - use a ForEach loop
    – DavidPostill
    Aug 1, 2022 at 16:51
  • 1
    @JustMe I'm not going to do that for you.
    – DavidPostill
    Aug 2, 2022 at 17:01
5

Use iconv from GNUWin32 pack. It is much faster, especially if your files are about or more than 1 Gb.

"C:\Program Files (x86)\GnuWin32\bin\iconv.exe" -f cp1251 -t utf-8 source.txt > result.txt
3
2

Here is for each convert *.text file to *.sql file:

foreach ($file in get-ChildItem *.txt) {
    Echo $file.name
    Get-Content $file | Set-Content -Encoding utf8 ("$file.name" +".sql")
 }
1
  • after run some sql file, sometime error, I decided to using linux with iconv command =)) May 28, 2019 at 15:46
1

You can do this from the command prompt as follows:

powershell -command "Get-Content .\test.txt" > test-utf8.txt

It turns out that piping the output to a file from the command prompt saves as utf-8.

1
  • but if I want to convert multiples .txt files, from a folder+subfolders, what is the command?
    – Just Me
    Aug 1, 2022 at 14:15
0

POWERSHELL: # Assumes Windows PowerShell, use -Encoding utf8BOM with PowerShell Core. For multiple files:

FIRST SOLUTION:

$files = Get-ChildItem c:\Folder1\ -Filter *.txt 

foreach ($file in $files) {

    Get-Content $file.FullName | Set-Content "E:\Temp\Destination\$($file.Name)" -Encoding utf8BOM

}

OR, SECOND SOLUTION (for multiple files):

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

OR, THE THIRD SOLUTION: (only for 2 files)

$a = "C:/Folder1/TEST_ro.txt"
 $b = "C:/Folder1/TEST_ro-2.txt"
 (Get-Content -path $a) | Set-Content -Encoding UTF8BOM -Path $b

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.