How to redirect output?

13,953

If you are writing output in script.ps1 using Write-Host (or [Console]::WriteLine) you will either need to change those to Write-Output or do this:

powershell.exe -File test.ps1 > out.txt

By the way > is syntactic sugar for Out-File, they are the same thing.

Share:
13,953
JBurace
Author by

JBurace

Updated on June 04, 2022

Comments

  • JBurace
    JBurace almost 2 years

    I am attempting to redirect output of a Powershell script to a txt file.

    In the Powershell window, I try:

    .\script.ps1 > list.txt

    But it does not help, all output still gets printed to the window.

    I then tried:

    .\script.ps1 >& list.txt

    And got this error:

    Missing file specification after redirection operator.
    At line:1 char:21
    + .\script.ps1 > <<<< & list.txt
        + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
        + FullyQualifiedErrorId : MissingFileSpecification
    
  • JBurace
    JBurace about 12 years
    The script uses [Console]::WriteLine
  • Andy Arismendi
    Andy Arismendi about 12 years
    @JBurace [Console]::WriteLine and Write-Host are basically the same thing. [Console]::Write is Write-Host -NoNewLine
  • Andy Arismendi
    Andy Arismendi about 12 years
    You probably mean Write-Host. Write-Output works fine with > list.txt.
  • Dominik Antal
    Dominik Antal over 10 years
    Now I'm getting : "Missing file specification after redirection operator." I tried a lot of variations I could think of :(