Powershell script/function to display text to the console in several colors and save it to log with time/date stamp


This is a handy function to include in scripts. It allows for sending text to the screen in several colors in the same line, and logs text to a log file with time/date stamp. To use it, download it from the Microsoft Script Center Repository, unblock the file, open it in Powershell_ISE, run it to load the function in memory, and use it.

Update: v1.1 – 12/01/2014 – added multi-color display in the same line

Example:

log Found,25,"files in folder",c:\ Green,Yellow,Green,Cyan

log1

Example:

Log -String "Hello World" -Color Yellow -LogFile c:\log.txt

This example displays the “Hello World” string to the console in yellow, and adds it as a new line to the file c:\log.txt. If c:\log.txt does not exist it will be created.

Log entries in the log file are time stamped.

log2Example:

Log "$((Get-Location).Path)" Cyan

This example displays current path in Cyan, and does not log the displayed text to log file.

log3Example:

$p = Get-Process | select -First 1
"$($p.name) process ID is $($p.id)" | log -color DarkYellow 

Sample output of this example:

log4

Example:

log Found,(Get-ChildItem -Path .\ -File).Count,"files in folder",(Get-Item .\).Ful
lName Green,Yellow,Green,Cyan .\mylog.txt

Sample output of this example:

log5

Advertisements

One response

  1. Pingback: Powershell module performs tasks related to StorSimple Hybrid Cloud Storage SAN | Sam's Corner

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s