hi.txt echo "hello worl...">

What is the difference between> operator and Set-Content cmdlet

I realized that these two lines:

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

don't do exactly the same job. I created a simple script that replaces the contents of some values ​​in a configuration file and saves it (using>), but it looks like the file is stored in some strange format. Standard Windows text editors see that the file is normal, but the IDE that should load this file (project configuration file) cannot read it (I think it uses some additional encoding or something else).

However, when I replaced it with Set-Content, it works great.

What is the default behavior of these commands, what does Set-Content do differently for it to work on this?

+4
source share
2 answers

The difference is which encoding is used by default. From MSDN we can see that Set-Contentthe default is ASCII encoding, which is read by most programs (but may not work if you do not write 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 ( link )

As a side note, you can also use Out-Fileone that uses Unicode encoding.

+5
source

The default Set-Content encoding is ASCII. This can be confirmed as follows:

Get-Help -Name Set-Content -Parameter Encoding;

PowerShell > - Unicode. , help about_Redirection PowerShell.

http://technet.microsoft.com/en-us/library/hh847746.aspx

+4

Source: https://habr.com/ru/post/1531773/


All Articles