在Windows PowerShell中回显主机文件在charactesr之间创build空格

以pipe理员模式运行以下命令Windows 8.1上的PowerShell会创build奇怪的输出:

PS C:\> cat C:\Windows\System32\drivers\etc\hosts # Copyright (c) 1993-2009 Microsoft Corp. # # This is a sample HOSTS file used by Microsoft TCP/IP for Windows. # 

以上是主机文件的初始内容。

 PS C:\> echo "127.0.0.1 example.com" >> C:\Windows\System32\drivers\etc\hosts 

我使用echo命令添加一行并…

 PS C:\> cat C:\Windows\System32\drivers\etc\hosts # Copyright (c) 1993-2009 Microsoft Corp. # # This is a sample HOSTS file used by Microsoft TCP/IP for Windows. # 1 2 7 . 0 . 0 . 1 example . com PS C:\> 

注意最后一行; 它在每个被回应的angular色之间都有空格。

任何想法为什么会发生?

看来显而易见的解决方案是不使用echo "text" >> file格式。 尝试使用添加内容:

 Add-Content -Path C:\windows\System32\drivers\etc\hosts. -Value "127.0.0.1 example.com" 

这会给你想要的输出。

我相信问题是PowerShell默认使用Unicode。

这将实现你正在做的事情:

 ECHO "127.0.0.1 example.com" | Out-File -Append -Encoding ASCII MyFile.txt