PowerShell Set-Content and Out-File - what is the difference?

In PowerShell, what's the difference between Out-File and Set-Content ? Or Add-Content and Out-File -append ?

I found that if I use both against the same file, the text is completely mojibaked .

(Minor second question: > this is an Out-File alias, right?)

+71
powershell
May 18 '12 at 3:48 p.m.
source share
5 answers

Here is a summary of what I’ve learned after months of working with PowerShell, and some science experiments. I never found any of this in the documentation :(

[ Update: Now a lot of this is better documented.]

Read and write lock

While Out-File running, another application may read the log file.

While Set-Content running, other applications cannot read the log file. Thus, never use Set-Content to register long commands.

coding

Out-File by default saves in Unicode ( UTF-16LE ) (although this can be specified), while Set-Content defaults to ASCII ( US-ASCII ) in PowerShell 3+ (this can also be specified). In earlier versions of PowerShell, Set-Content recorded default encoded content (ANSI).

Editor 's Note: PowerShell in version 5.1 still defaults to the culture of the specific Default ("ANSI") encoding, despite the fact that it claims to documentation. If ASCII were the default, would non-ASCII characters such as ü be converted to literals ? but it is not: 'ü' | Set-Content tmp.txt; (Get-Content tmp.txt) -eq '?' 'ü' | Set-Content tmp.txt; (Get-Content tmp.txt) -eq '?' 'ü' | Set-Content tmp.txt; (Get-Content tmp.txt) -eq '?' gives $False .

 PS > $null | out-file outed.txt PS > $null | set-content set.txt PS > md5sum * f3b25701fe362ec84616a93a45ce9998 *outed.txt d41d8cd98f00b204e9800998ecf8427e *set.txt 

This means that the default values ​​for the two commands are incompatible, and mixing them will damage the text, so always specify the encoding.

formatting

As Bartek explained, Out-File retains unusual output formatting, as seen in the terminal. So, in the folder with two files, the dir | out-file out.txt dir | out-file out.txt dir | out-file out.txt dir | out-file out.txt creates a file with 11 lines.

Whereas Set-Content retains a simpler view. In this folder with two files, the dir | set-content sc.txt dir | set-content sc.txt dir | set-content sc.txt dir | set-content sc.txt creates a file with two lines. To emulate output in a terminal:

 PS > dir | ForEach-Object {$_.ToString()} out.txt sc.txt 

I believe that this formatting is important for line breaks, but I still cannot describe it.

File creation

Set-Content does not create a reliable empty file if Out-File :

In an empty folder, the dir | out-file out.txt command dir | out-file out.txt dir | out-file out.txt dir | out-file out.txt dir | out-file out.txt creates the file, and dir | set-content sc.txt dir | set-content sc.txt dir | set-content sc.txt dir | set-content sc.txt does not have.

Variable pipeline

Set-Content takes the file name from the pipeline; allowing you to set some fixed value for the contents of a number of files.

Out-File receives data from the pipeline; updating the contents of a single file.

options

Set-Content includes the following additional parameters:

  • exclude
  • Filter
  • Include
  • Pass the
  • Flow
  • UseTransaction

Out-File includes the following additional options:

  • join
  • Noclobber
  • width

For more information on what these options are, please refer to the help; e.g. get-help out-file -parameter append .

+78
Nov 24 '12 at 18:10
source share
— -

Out-File has an overwrite behavior for the output path if the -NoClobber and / or -Append flag is not set. Add-Content will add content if the output path already exists by default (if it can). Both will create a file if it does not already exist.

Another interesting difference is that Add-Content will create an ASCII encoded Out-File by default, and Out-File will create a little endic unicode encoded Out-File by default.

> is an alias syntax sugar for Out-File . It is an Out-File with some predefined parameter settings.

+15
May 18 '12 at 16:04
source share

Well, I would not agree ... :)

  • Out-File has -Append (-NoClober to avoid overwriting), which will add-Content. But this is not the same beast.
  • team | Add-Content will use the .ToString () method for input. Out-File will use the default formatting.

So:

 ls | Add-Content test.txt 

and

 ls | Out-File test.txt 

will give you completely different results.

And no, '>' is not an alias, it is a redirection operator (the same as in other shells). And it has very serious limitations ... It will cut the lines the same way they are displayed. Out-File has the -Width option to help you avoid this. In addition, with redirection operators you cannot decide which encoding to use.

NTN Bartek

+7
May 18 '12 at 18:43
source share

Set-Content supports -Encoding Byte , but Out-File does not.

Therefore, when you want to write binary data or the result of Text.Encoding#GetBytes() to a file, you must use Set-Content .

+3
May 30 '17 at 15:17
source share

The outgoing -append or ">>" file can actually mix two encodings in one file. Even if the file is originally ascii or ansi, it will by default add unicode to the end of the file. Add-content will check the encoding and match it before adding. By the way, exportii-csv defaults to ascii (without accents) and set-content / add-content for ansi.

0
May 11 '19 at
source share



All Articles