3

So I'm trying to write a quick script to help my son study for his spelling bee. I've got almost everything working, but I'd like to track his results, so I'm trying to write to output to a text file. I only care about the most recent results, so I delete the existing results file when he starts the test.

I'm doing a for each for his spelling list, and at the end of each, I have either:

write-host "Word $counter - You spelled $wordcorrectly!!" | Out-File $outputpath -Append

or

write-host "Word $counter - Sorry.  The correct spelling is $word." | Out-File $outputpath -Append

Finally, after it goes through all the list, I have:

write-host "You answered $counter out of $WordCount correctly" | Out-File $outputpath -Append

But when I go to the $outputpath file, it's completely blank... Any guess on what simple mistake I'm making here?

Christopher Cass
  • 817
  • 4
  • 19
  • 31

1 Answers1

6

You're piping the result of Write-Host ... to Out-File, but Write-Host doesn't pass anything to the pipeline (it sends things to standard output), so the Out-File is doing nothing (except overwrite the file). So instead, you can pipe the string itself to Out-File and it should work. If you still want to Write-Host the string (to see the output), you can store the message in a variable:

$Message = "You answered $counter out of $WordCount correctly"
Write-Host $Message
$Message | Out-File $outputpath -Append
Nathan W
  • 283
  • 1
  • 6