Solved

PowerShell Write to Screen in Color and Log FIle

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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

In this previous article (https://oddytee.wordpress.com/2016/05/05/provision-new-office-365-user-and-mailbox-from-exchange-hybrid-via-powershell/), we made basic license assignments to users in O365. When I say basic, the method is the simplest way …
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…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

895 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

15 Experts available now in Live!

Get 1:1 Help Now