Friday, March 8, 2019

Change font color in Power-Shell and display in PS console

Every want color full life and color full job. Some time watching a black and white console makes thing very boring bring some color in your power shell console. Change font color using the power shell script. Its always looks good when you code have some significant color in console .As a script-er you have to show some nice color in power shell scripts.



In order to change the colors, I’ll familiarize myself with where that info is stored.This info is stored in the variable $host

you want to be cool and use Power-Shell to list the enum values.

1.
[System.Enum]::GetValues('ConsoleColor') | ForEach-Object { Write-Host $_ -ForegroundColor $_ }


2.  Change the font color of the console

      $host.UI.RawUI.ForegroundColor = "DarkGreen"

3.  Changing the main console foreground and background is slightly different. That data is stored in       a separate location.

      $host.UI.RawUI.ForegroundColor = "DarkGreen"
      $host.UI.RawUI.BackgroundColor = "Black"

4. Write with different text and background colors

     Write-Host "Whole text is in red" -ForegroundColor Red
     Write-Host "Red on white text." -ForegroundColor green -BackgroundColor black

5. Write to the console without adding a new line

     Write-Host "no newline test " -NoNewline
     Write-Host "second string"

6. Suppress output from Write-Host

    Write-Host "I won't print" -InformationAction Ignore

7. Color full console 

   Write-Host "Green " -ForegroundColor Green -NoNewline; 
   Write-Host "Red " -ForegroundColor Red -NoNewline; 
   Write-Host "Yellow " -ForegroundColor Yellow -NoNewline;





Note:- Please test script in your test environment before running in production directly.

No comments:

Post a Comment