Write-Color is a wrapper around Write-Host allowing you to create nice looking scripts, with colorized output.
Write-Color is a wrapper around Write-Host allowing you to create nice looking scripts, with colorized output.
# Install from Powershell Gallery https://www.powershellgallery.com/packages/PSWriteColor # Install-Module -Name PSWriteColor Import-Module PSWriteColor Clear-Host # Example 1 Write-Color "[i] ", "Parameter in configuration of ", "EmailParameters.EmailFrom", " exists." -Color White, White, Green, White -ShowTime Write-Color "[i] ", "Parameter in configuration of ", "EmailParameters.EmailTo", " exists." -Color White, White, Green, White -ShowTime # Example 2 Write-Color "[i] ", "I will send email soon...", "Get ready.." -Color White Write-Color "[i] ", "Sending email...." -Color White, White -NoNewLine <# Do Something.... #> if ($true) { Write-Color -Text "OK" -Color Green } # Example 3 Write-Color -Text "Red ", "Green ", "Yellow " -Color Red, Green, Yellow Write-Color -Text "This is text in Green ", "followed by red ", "and then we have Magenta... ", "isn't it fun? ", "Here goes DarkCyan" -Color Green, Red, Magenta, White, DarkCyan Write-Color -Text "This is text in Green ", "followed by red ", "and then we have Magenta... ", "isn't it fun? ", "Here goes DarkCyan" -Color Green, Red, Magenta, White, DarkCyan -StartTab 3 -LinesBefore 1 -LinesAfter 1 Write-Color "1. ", "Option 1" -Color Yellow, Green Write-Color "2. ", "Option 2" -Color Yellow, Green Write-Color "3. ", "Option 3" -Color Yellow, Green Write-Color "4. ", "Option 4" -Color Yellow, Green Write-Color "9. ", "Press 9 to exit" -Color Yellow, Gray -LinesBefore 1 Write-Color -LinesBefore 2 -Text "This little ", "message is ", "written to log ", "file as well." ` -Color Yellow, White, Green, Red, Red -LogFile "C:\testing.txt" -TimeFormat "yyyy-MM-dd HH:mm:ss" Write-Color -Text "This can get ", "handy if ", "want to display things, and log actions to file ", "at the same time." ` -Color Yellow, White, Green, Red, Red -LogFile "C:\testing.txt" # Example 4 with backgrund colors and usage of aliases Write-Color -T "My text", " is ", "all colorful" -C Yellow, Red, Green -B Green, Green, Yellow Write-Color -T "My text", " is ", "all colorful" -C Yellow, Red, Green -B Red, Green, Green # Example 5 with aliases wc -t "my text" -C Red
And the results…
While you can use the script in a standard way by downloading it, putting it in right places, into your scripts, copy/pasting and getting it to run .. there is much simpler way. Since the script was published to PowerShell Gallery you can simply install the module and run it from anywhere. Just use Install-Module PSWriteColor. And it just magically works…
Install-Module -Name "PSWriteColor"
Accept untrusted repository (PowerShellGallery is owned by Microsoft), and finally accept installation of module. And that's it… you're free to use Write-Color in any of your scripts. No need to play with copying files (except for the configuration of reporting of course).
What is great about this method is that when I'll update PowerShell Module to new version… all you have to do is …
Update-Module -Name "PSWriteColor"
It will install new version of Write-Color but it will leave the old one in place. So you can always revert back. If you want to keep only the latest…
Uninstall-Module -Name "PSWriteColor" -AllVersions Install-Module -Name "PSWriteColor"