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


2 May 2020:

Microsoft is retiring the Technet Gallery as of June 2020. This function has been incorporated into the AZSBTools PowerShell module available in the PowerShell Gallery as the Write-Log function.


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

3 responses

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

  2. Thank you for the auspicious writeup. It in fact was a amusement account it. Look advanced to far added agreeable from you! By the way, how can we communicate?

    April 7, 2020 at 8:40 am

  3. Hi there would you mind sharing which blog platform you’re working with? I’m going to start my own blog in the near future but I’m having a tough time making a decision between BlogEngine/Wordpress/B2evolution and Drupal. The reason I ask is because your design seems different then most blogs and I’m looking for something unique. P.S My apologies for being off-topic but I had to ask!

    May 2, 2020 at 6:24 am

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.