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:
For more information on what these options are, please refer to the help; e.g. get-help out-file -parameter append .