Share via


Running function with "Write-Host" and redirect output to file does nothing.

Question

Wednesday, September 19, 2012 1:18 PM

Hello,

I created this function:

function TicketSolutionInformation {
Write-Host "User has been created." -ForegroundColor Green
}
TicketSolutionInformation | Out-File -filepath ("123" + ".txt") -append

but the file 123.txt is always empty. Why ?

All replies (9)

Wednesday, September 19, 2012 1:33 PM ✅Answered | 1 vote

Write-Host will write text to console, not pipepeline so you'll always get nothing.

Consider changing function to this:

function TicketSolutionInformation {
Param(
 [switch]$Passthru
)
Write-Host "User has been created." -ForegroundColor Green
if($Passthru){
"User has been created."
}
}TicketSolutionInformation -Passthru | Out-File -filepath ("123" + ".txt") -append

Wednesday, September 19, 2012 1:45 PM ✅Answered | 1 vote

Alternatively, you could use Tee-Object to write to the screen and append to an output file at the same time.

Grant Ward, a.k.a. Bigteddy


Wednesday, September 19, 2012 1:38 PM

Is there any other way to do this, without need to rewrite all of my functions ? And what about variables  like: ?

...
Write-Host "User has been created:$User" -ForegroundColor Green
...

Will they include in file as well ?


Wednesday, September 19, 2012 1:43 PM | 1 vote

Change all your Write-Hosts to Write-Output.  Unfortunately, you won't get any console output if you pipe the output to Out-File.

Grant Ward, a.k.a. Bigteddy


Thursday, September 20, 2012 4:50 AM

Cannot do that. Replacing "Write-Host" to "Write-Output" makes all "-foregroundColor" switches as "output". Maybe there is a way to get rid of them in output ?


Thursday, September 20, 2012 4:50 AM

Thx. I will try use Tee-Object.


Thursday, September 20, 2012 3:30 PM

write-host did not do all that you wanted, so you were shown that you should use write-output instead. But since the two cmdlets support a different set of parameters, you need to change more than just the name of the cmdlet being called.

You could write a function called "write-output" that calls the cmdlet "write-output" to output all input parameters UNLESS they are equal to "-foregroundcolor" or "green". But to fully debug that would be, in my opinion, more work than just re-writing your code so that it funtions the way you want it to.

Al Dunbar -- remember to 'mark or propose as answer' or 'vote as helpful' as appropriate.


Tuesday, August 19, 2014 6:06 PM | 1 vote

not sure if this help.

I found it at time when I like to alert a user who run my program to see alert on the console and also | it to a file so if the user needed to leave for the out house. then she/he can check the file.

here my testing code.

PS C:\Users\administrator.CORP> $1a = "john"
PS C:\Users\administrator.CORP> $1b = "Li"
PS C:\Users\administrator.CORP> $1c = "My name is $1a $1b."
PS C:\Users\administrator.CORP> $1c | tee-object -filepath c:\myscripts\jonli.txt -append
My name is john Li.
PS C:\Users\administrator.CORP> $1c | tee-object -filepath c:\myscripts\jonli.txt -append
My name is john Li..

er, I do get a text file named joeli.txt with 2 line

jon


Monday, September 21, 2015 2:27 AM

Hi..

I'd prefer write my code like this:

$OutputFileLocation = "D:\Logs\MyLogs-$(get-date -uformat '%Y-%m-%d-%H_%M').log"
Write-Host "START logging activity"
Write-Output "START logging activity" | Out-File $OutputFileLocation -Append
Write-Host "Put another debug text here " + $yourvariable
Write-Output ("Put another debug text here " + $yourvariable) | Out-File $OutputFileLocation -Append

Regards,

HENDHY