Write output to a text file in PowerShell

后端 未结 3 1733
无人共我
无人共我 2020-12-01 05:00

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)         


        
相关标签:
3条回答
  • 2020-12-01 05:32

    Use the Out-File cmdlet

     Compare-Object ... | Out-File C:\filename.txt
    

    Optionally, add -Encoding utf8 to Out-File as the default encoding is not really ideal for many uses.

    0 讨论(0)
  • 2020-12-01 05:34

    The simplest way is to just redirect the output, like so:

    Compare-Object $(Get-Content c:\user\documents\List1.txt) $(Get-Content c:\user\documents\List2.txt) > c:\user\documents\diff_output.txt
    

    > 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.

    0 讨论(0)
  • 2020-12-01 05:46

    Another way this could be accomplished is by using the Start-Transcript and Stop-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.

    0 讨论(0)
提交回复
热议问题