35

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?

6 Answers 6

50

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

This method will convert to UTF-8-BOM


Further Reading

13
  • 1
    @vibs2006 Use a ForEach loop. See ForEach - PowerShell - SS64.com.
    – DavidPostill
    Commented Jan 28, 2019 at 18:32
  • 2
    Will this method convert to plain UTF-8 or UTF-8-BOM? Commented Oct 23, 2019 at 22:28
  • 1
    but if I want to convert multiples .txt files, from a folder+subfolders, what is the command?
    – Just Me
    Commented Aug 1, 2022 at 14:15
  • 1
    @JustMe See my comment above - use a ForEach loop
    – DavidPostill
    Commented Aug 1, 2022 at 16:51
  • 1
    yes, but is much easy to put the entire formula here
    – Just Me
    Commented Aug 2, 2022 at 16:25
10

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
3

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 =)) Commented May 28, 2019 at 15:46
2

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
    Commented Aug 1, 2022 at 14:15
1

For those who want to batch convert several files (e.g.: all *.txt files in folder and sub-folders):

dir *.txt -Recurse | foreach {
  # May remove the line below if you are confident
  Copy-Item $_ $_.bkp
  
  # Note that since we are reading and saving to the same file,
  # we need to enclose the command in parenthesis so it fully executes 
  # (reading all content and closing the file) before proceeding
  (Get-Content $_) | Set-Content -Encoding utf8 $_
}
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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .