?
Solved

PowerShell Write to Screen in Color and Log FIle

Posted on 2014-07-23
4
Medium Priority
?
2,918 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 1200 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

WatchGuard's M Series Appliances - Miecom Approved

WatchGuard's newest M series appliances were put to the test by Miercom.  We had great results and outperformed all of our competitors in both stateless and stateful traffic throghput scenarios! Ready to see how your UTM appliance stacked up? Download the Miercom Report!

Question has a verified solution.

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

A procedure for exporting installed hotfix details of remote computers using powershell
In this post we will be converting StringData saved within a text file into a hash table. This can be further used in a PowerShell script for replacing settings that are dynamic in nature from environment to environment.
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ā€¦
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastrā€¦

762 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