Solved

PowerShell Write to Screen in Color and Log FIle

Posted on 2014-07-23
4
2,443 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

Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I thought I'd write this up for anyone who has a request to create an anonymous whistle-blower-type submission form created using SharePoint 2010 (this would probably work the same for 2013). It's not 100% fool-proof but it's as close as you can get…
"Migrate" an SMTP relay receive connector to a new server using info from an old server.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

825 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