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

mrwjdhj3  于 2023-03-19  发布在  Windows
关注(0)|答案(3)|浏览(119)

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

注意最后一行;它在每个被回显的字符之间都有空格。
知道为什么会这样吗?

drnojrws

drnojrws1#

看起来最明显的解决方案是不使用echo "text" >> file。尝试使用add-content代替:

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

这将为您提供所需的输出。

vbkedwbf

vbkedwbf2#

我认为问题是PowerShell默认使用Unicode。
这将实现您正在尝试做的事情:

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

46scxncf3#

在CMD中,而不是PS中:
回显IP地址主机名域名〉〉%系统驱动器%\Windows\System32\驱动程序\etc\主机
即:
返回127.0.0.1microsoft.com〉%系统驱动器%\Windows\System32\驱动程序\等\主机

相关问题