回显到Windows PowerShell中的主机文件会在字符之间创建空格ESR

回显到Windows PowerShell中的主机文件会在字符之间创建空格ESR,windows,powershell,echo,Windows,Powershell,Echo,在Windows 8.1上以管理员模式PowerShell运行以下命令会创建奇怪的输出: 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. # 上面是hosts文件的初始内容 PS C:\> echo "127.

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

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.
#
上面是hosts文件的初始内容

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   e x a m p l e . c o m     

PS C:\>
注意最后一行;它在每个被回响的字符之间都有空格


知道为什么会发生这种情况吗?

显然,解决办法是不使用
echo“text”>>文件
格式。尝试改用“添加内容”:

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