I've compared two files using the following code:
Compare-Object $(Get-Content c:\user\documents\List1.txt) $(Get-Content c:\user\documents\List2.txt)
How can I write the output of this to a new text file? I've tried using an echo command, but I don't really understand the syntax.
Another way this could be accomplished is by using the
Start-Transcript
andStop-Transcript
commands, respectively before and after command execution. This would capture the entire session including commands.Start-Transcript
Stop-Transcript
For this particular case
Out-File
is probably your best bet though.The simplest way is to just redirect the output, like so:
>
will cause the output file to be overwritten if it already exists.>>
will append new text to the end of the output file if it already exists.Use the
Out-File
cmdletOptionally, add
-Encoding utf8
toOut-File
as the default encoding is not really ideal for many uses.