In PowerShell, what's the difference between Out-File
and Set-Content
? Or Add-Content
and Out-File -append
?
I've found if I use both against the same file, the text is fully mojibaked.
(A minor second question: >
is an alias for Out-File
, right?)
Set-Content (Microsoft.PowerShell.Management) - PowerShell Set-Content is a string-processing cmdlet that writes new content or replaces the content in a file. Set-Content replaces the existing content and differs from the Add-Content cmdlet that appends content to a file.
Description. The Out-File cmdlet sends output to a file. It implicitly uses PowerShell's formatting system to write to the file.
Use Get-Content and Set-Content to Replace Every Occurrence of a String in a File With PowerShell. The Get-Content gets the item's content in the specified path, such as the text in a file. The Set-Content is a string-processing cmdlet that allows you to write new content or replace the existing content in a file.
The Get-Content cmdlet gets the content of the item at the location specified by the path, such as the text in a file or the content of a function. For files, the content is read one line at a time and returns a collection of objects, each of which represents a line of content.
Here's a summary of what I've deduced, after a few months experience with PowerShell, and some scientific experimentation. I never found any of this in the documentation :(
[Update: Much of this now appears to be better documented.]
While Out-File
is running, another application can read the log file.
While Set-Content
is running, other applications cannot read the log file. Thus never use Set-Content
to log long running commands.
Out-File
saves in the Unicode
(UTF-16LE
) encoding by default (though this can be specified), whereas Set-Content
defaults to ASCII
(US-ASCII
) in PowerShell 3+ (this may also be specified). In earlier PowerShell versions, Set-Content
wrote content in the Default
(ANSI) encoding.
Editor's note: PowerShell as of version 5.1 still defaults to the culture-specific Default
("ANSI") encoding, despite what the documentation claims. If ASCII were the default, non-ASCII characters such as ü
would be converted to literal ?
, but that is not the case: 'ü' | Set-Content tmp.txt; (Get-Content tmp.txt) -eq '?'
yields $False
.
PS > $null | out-file outed.txt PS > $null | set-content set.txt PS > md5sum * f3b25701fe362ec84616a93a45ce9998 *outed.txt d41d8cd98f00b204e9800998ecf8427e *set.txt
This means the defaults of two commands are incompatible, and mixing them will corrupt text, so always specify an encoding.
As Bartek explained, Out-File
saves the fancy formatting of the output, as seen in the terminal. So in a folder with two files, the command dir | out-file out.txt
creates a file with 11 lines.
Whereas Set-Content
saves a simpler representation. In that folder with two files, the command dir | set-content sc.txt
creates a file with two lines. To emulate the output in the terminal:
PS > dir | ForEach-Object {$_.ToString()} out.txt sc.txt
I believe this formatting has a consequence for line breaks, but I can't describe it yet.
Set-Content
doesn't reliably create an empty file when Out-File
would:
In an empty folder, the command dir | out-file out.txt
creates a file, while dir | set-content sc.txt
does not.
Set-Content
takes the filename from the pipeline; allowing you to set a number of files' contents to some fixed value.
Out-File
takes the data as from the pipeline; updating a single file's content.
Set-Content
includes the following additional parameters:
Out-File
includes the following additional parameters:
For more information about what those parameters are, please refer to help; e.g. get-help out-file -parameter append
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With