呼应到hosts文件在Windows PowerShell中创建

问题描述:

charactesr之间的空间在Windows 8.1在管理员模式PowerShell中运行以下命令创建怪异输出:呼应到hosts文件在Windows 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. 
# 

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

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" >> 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