Solved

PowerShell Write to Screen in Color and Log FIle

Posted on 2014-07-23
4
2,272 Views
Last Modified: 2014-07-23
Hi,

How can I write a message to the screen WITH color and also to a log file?   Is there a simple line of code in PowerShell to do this?   I found some examples online but it didn't allow me to change the font color on the screen.

David
0
Comment
Question by:davidst98
  • 2
4 Comments
 
LVL 39

Accepted Solution

by:
footech earned 300 total points
ID: 40215008
The easiest way to change the font color is with Write-Host, but that only outputs to the console and can't be redirected to a file.  It is also possible to change the colors used by the console for the foreground and background.
One possibility is to use Tee-Object like below.
"some text" | Tee-Object -FilePath output.txt | Write-Host -ForegroundColor Yellow

Open in new window

0
 

Author Comment

by:davidst98
ID: 40215117
Actually I'm using Powershell 2.0 and I can't append using Tee-Object.
0
 
LVL 16

Expert Comment

by:Enphyniti
ID: 40215277
The way I do it is with two lines.

$logfile = "X:\log.txt"  
Write-host -f color "Stuff to go to the console"
write "stuff to go to log" | out-file -append $logfile


Maybe not the most elegant, but it works.  It also allows me to include different information in the log than on the console.  In an interactive script, you may want to be more verbose, but only want the log to contain a record of specific actions.

I usually write my scripts entirely with write-host while developing and then go through after they're complete to determine what I want to log with write statements.
0
 
LVL 39

Expert Comment

by:footech
ID: 40215286
If you need to be able to append, you could use
"some text" | Add-Content -path output2.txt -PassThru | Write-Host -ForegroundColor Yellow

Open in new window

More often I would say that the on-screen output differs from that sent to file, in which case you'll just need to use separate commands to write to a file and to output to console (meaningless example below).
1..3 | % {
  Write-Host "Processing $_" -foregroundcolor yellow
  Add-Content -value "Number is $_" -path output.txt
}

Open in new window

0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Hi all.   The other day I had to change the passwords for a bunch of users on the fly. Because they were so many, I decided to do it in an automated way and I would like to share it with you all.   If you are not doing it directly in a Domain Co…
Synchronize a new Active Directory domain with an existing Office 365 tenant
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now