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
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.
Log "$((Get-Location).Path)" Cyan
This example displays current path in Cyan, and does not log the displayed text to log file.
$p = Get-Process | select -First 1 "$($p.name) process ID is $($p.id)" | log -color DarkYellow
Sample output of this example:
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:
Related
This entry was posted on December 1, 2014 by SamB. It was filed under Powershell and was tagged with log with timestamp, output text in several colors, Powershell.
3 responses
Leave a comment Cancel reply
This site uses Akismet to reduce spam. Learn how your comment data is processed.
Pingback: Powershell module performs tasks related to StorSimple Hybrid Cloud Storage SAN | Sam's Corner
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
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