set-content
creates or replaces a file and writes this file's text. In its most basic form, the
cmdLet is given a file name and an
array whose elements represent the lines in the file. The following example creates (or replaces) the file
just-some.text
:
DOS and Unix line endings
By default,
set-content
uses DOS
line endings when writing a file (at least on
Windows, that is).
In order to create a file that has Unix line endings, the array with the lines must first be
joined with the Unix separator ("`n") and
then written.
Because set-content
by default also ends the file with a line separator, the -noNewLine
must also be used. The final new line needs then to be added «manually», too.
$lines = 'foo', 'bar', 'baz'
set-content dos.txt $lines
set-content unix.txt (($lines -join "`n") + "`n" ) -noNewLine
format-hex dos.txt
format-hex unix.txt
Note, it seems that -noNewLine
must appear as last element in this command as it causes an error if it directly follows set-content
.