37

Is it possible to save a text file in UTF-8 encoding using Windows' command line cmd.exe?

Current I'm creating the text file using command redirection operators:

C:\Windows\system32\ipconfig /all >> output.log

I start with > to get a new file and then I use >> to append more information.

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
user2333346
  • 1,083
  • 4
  • 21
  • 40
  • 3
    If you are using I/O redirection operators, then it's not up to the shell what the character encoding of the data is. It's up to the command whose output is redirected. If that command produces valid UTF-8, that's what will end up in the file. – Celada May 17 '13 at 00:29
  • Imo `ipconfig` uses "Unicode". – Endoro May 17 '13 at 04:36
  • It doesn't. Like all console mode programs, it generates text in the default 8-bit code page encoding that's active for the console. Which you change to utf-8 with chcp 65001. You still won't get a BOM. – Hans Passant May 17 '13 at 10:03
  • 2
    `cmd /u /c "ipconfig /all" >> output.log` ??? – npocmaka May 17 '13 at 18:46

2 Answers2

49

The default encoding for command prompt is Windows-1252. Change the code page (chcp command) to 65001 (UTF-8) first and then run your command.

chcp 65001
C:\Windows\system32\ipconfig /all >> output.log

Change it back to default when done.

chcp 1252
SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
rchn
  • 973
  • 1
  • 8
  • 10
4

As the existing answer says, in a batch file, you can use the chcp command

chcp 65001 > nul
some_command > file

But if you are using the cmd.exe from its command line, e.g. to execute a user-defined command, you can use this syntax:

cmd.exe /c chcp 65001 > nul & cmd.exe /c some_command > file
Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992