Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Powershell display message both on console and in a file

Not sure if this is a simple question or some code is required in order to achieve this, so here it goes.

I have a script which basically displays some text 1st on the PS console and also in a log file.

The code I have uses both the write-host and write-output, so my question is that if the 2 can be combined somehow? I want only specific message to be logged, so doing a PS transcript will not work.

Write-host "Error: whatever message" 
Write-Output "Error: whatever message" | Out-File -Append $Log_file

Thanks in advance!

like image 818
user3144292 Avatar asked Dec 29 '13 21:12

user3144292


1 Answers

Simple answer is to use a Tee-Object. In version 3 you should have access to the -Append parameter as well (but not in v2)

Write-Output "Error: whatever message" | Tee-Object -Append $Log_file
like image 106
Knuckle-Dragger Avatar answered Oct 01 '22 02:10

Knuckle-Dragger