2014-09-04 44 views
1

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:\> 

通知的最後一行;它在每個被迴應的角色之間都有空格。

任何想法爲什麼會發生這種情況?

回答

4

似乎顯而易見的解決方案是不使用echo "text" >> file格式。嘗試使用添加內容代替:

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

這會給你想要的輸出。

2

我相信問題是PowerShell默認使用Unicode。

這將實現你正在試圖做的事:

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