Solved

PowerShell Write to Screen in Color and Log FIle

Posted on 2014-07-23
4
2,757 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 40

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 40

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

A brief introduction to what I consider to be the best editor for PowerShell.
Auditing domain password hashes is a commonly overlooked but critical requirement to ensuring secure passwords practices are followed. Methods exist to extract hashes directly for a live domain however this article describes a process to extract u…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

717 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