> 和有什么区别?运算符和 Set-Content cmdlet [英] What is a difference between > operator and Set-Content cmdlet

查看:31
本文介绍了> 和有什么区别?运算符和 Set-Content cmdlet的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现这两行:

echo "hello world" > hi.txt
echo "hello world" | Set-Content hi.txt

没有做完全相同的工作.我创建了一个简单的脚本,它替换配置文件中某些值的内容并将其存储(使用 >),但这似乎以某种奇怪的格式存储文件.标准 Windows 文本编辑器确实可以正常看到该文件,但是应该加载此文件的 IDE(它是项目的配置文件)无法读取它(我认为它使用了一些额外的编码或其他什么).

aren't doing exactly the same job. I created a simple script that replaces a content of some values in a configuration file and store it (using >), but that seems to store file in some weird format. Standard windows text editors do see the file normal, but the IDE which is supposed to load this file, (it's configuration file of a project) is unable to read it (I think it uses some extra encoding or whatever).

然而,当我用 Set-Content 替换它时,它工作正常.

However when I replaced it with Set-Content it works fine.

这些命令的默认行为是什么,Set-Content 做了什么不同的事情,以便在其中起作用?

What is a default behaviour of these commands, what Set-Content does differently so that it works in that?

推荐答案

不同之处在于使用的默认编码.从MSDN,我们可以看到Set-Content默认为ASCII编码,大多数程序都可以读取(但如果您不写英语,则可能无法使用).另一方面,> 输出重定向运算符与 Powershell 的内部字符串表示一起使用,即 .Net System.String,即 UTF-16(参考)

The difference is in what default encoding is used. From MSDN, we can see that Set-Content defaults to ASCII encoding, which is readable by most programs (but may not work if you're not writing english). The > output redirection operator on the other hand works with Powershell's internal string representation, which is .Net System.String, which is UTF-16 (reference)

作为旁注,您还可以使用 Out-File,它使用 unicode 编码.

As a side-note, you can also use Out-File, which uses unicode encoding.

这篇关于> 和有什么区别?运算符和 Set-Content cmdlet的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆