DarkLite1
DarkLite1

Reputation: 14695

Redirect all output to a file in PowerShell

I want to redirect all output to a file, like this:

Write-Error 'bla' *>>  '\\SERVER\s$\Test\test.log'

The problem is when I want to use a LiteralPath, then it fails as it's trying to resolve the path as a wildcard path. So this fails:

Write-Error 'bla' *>>  '\\SERVER\s$\Test\test[0].log'

When trying Write-Error 'bla' | Out-File -LiteralPath it doesn't write the error to the file, but only to the console.

Is there a way to have all the output in the file when using special characters in the file name?

Upvotes: 6

Views: 14860

Answers (2)

DarkLite1
DarkLite1

Reputation: 14695

I think I've found it:

Write-Error 'bla' 2>&1 | Out-File -LiteralPath '\\SERVER\s$\Test\test[0].log' -Append

Upvotes: 7

Chris
Chris

Reputation: 1009

You could use the Start-Transcript and Stop-Transcript cmdlets:

Start-Transcript -Path '\\SERVER\s$\Test\test[0].log'
# Your code
Stop-Transcript

Upvotes: 8

Related Questions