2

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

3 Answers 3

3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Now I'm getting : "Missing file specification after redirection operator." I tried a lot of variations I could think of :(
0

If the output you're wanting to capture is being written to the console using Write-Host, you need to change that to Write-Output.

2 Comments

The script uses [Console]::WriteLine
@JBurace [Console]::WriteLine and Write-Host are basically the same thing. [Console]::Write is Write-Host -NoNewLine
0

You don't need the & after the >. It is only used to execute something.

.\script.ps1 > list.txt

If script.ps1 is outputting using Write-Host or [Console]::WriteLine you will need to update it.

Here is an example of updating a Write-Host script to be outputable.

1 Comment

You probably mean Write-Host. Write-Output works fine with > list.txt.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.