Mihail
Mihail

Reputation: 9

Powershell: Trying to change back/foreground colors to text output (no Write-Host)

As the title suggests, I'm trying to change color of the fore/background of text outputed by: {$_.Name +" Drive: Used: "+"{0:N2}" -f($_.Used/1gb) + " Free: "+"{0:N2}" -f($_.Free/1gb) + " Total: "+"{0:N2}" -f(($_.used/1gb) + ($_.Free/1gb));

Where Drive, Used, Free, Total text output is I would like to add some foreground and background, but I cannot figure out how can I do that.

I'm trying to poke the script from internet in order to learn some new things with Powershell.

Full script: Get-PSDrive | Where-Object{$_.Free -gt 1} | ForEach-Object{$count = 0 ; "`n" }{$_.Name + " Drive: Used: " +"{0:N2}" -f($_.Used/1gb) + " Free: "+"{0:N2}" -f($_.Free/1gb) + " Total: "+"{0:N2}" -f(($_.used/1gb) + ($_.Free/1gb)); $count = $count + $_.Free;}{Write-Host "Total Free Space: " ("{0:N2}" -f($count/1gb)) -ForegroundColor White -BackgroundColor Black}

Upvotes: 0

Views: 1558

Answers (3)

stansy
stansy

Reputation: 308

For PowerShell 7.2 you can easily use the automatic variable $PSStyle:

$FG_LIGHT_BLUE = $PSStyle.Foreground.FromRgb(0x0099ff)
$Message = "Light blue text"
Write-Host "`r$FG_LIGHT_BLUE$Message$($PSStyle.Reset)"

Upvotes: 0

postanote
postanote

Reputation: 16086

One can do color without Write-host by using...

PSHostUserInterface.RawUI Property

$host.UI.RawUI.ForegroundColor = 'YourColorChoice'
$host.UI.RawUI.BackgroundColor = 'YourColorChoice'

... in all PowerShell versions. See this article.

Or using the .Net namespace...

[console]::ForegroundColor = 'YourColorChoice'
[console]::BackgroundColor = 'YourColorChoice'

... in all PowerShell versions. See this article.

You've already been pointed to the Ansi/VT stuff in PowerShell v5x.

either write your own functions to use those or know that there are several modules available to address this use case.

Find-Module -Name '*color*' | Format-Table -AutoSize
<#
# Results
Version Name                    Repository Description                                                                                                                     
------- ----                    ---------- -----------                                                                                                                     
0.87    PSWriteColor            PSGallery  Write-Color is a wrapper around Write-Host allowing you to create nice looking scripts, with colorized output. It provides ea...
2.2.0   Get-ChildItemColor      PSGallery  Get-ChildItemColor provides colored versions of Get-ChildItem Cmdlet and Get-ChildItem | Format-Wide (ls equivalent)            
1.5     ISEColorTheme.Cmdlets   PSGallery  A collection of Powershell functions that expand the PowerShell ISE themeing capability to the command line. These functions ...
1.0.0.0 PSColor                 PSGallery  Provides basic color highlighting for files, services, select-string etc....                                                    
0.1.0.0 AnsiColorOut            PSGallery  ANSI color escape sequences for console output.                                                                                 
1.1.2   DirColors               PSGallery  Provides dircolors-like functionality to all System.IO.FilesystemInfo formatters                                                
1.3.0   PSColors                PSGallery  Nice prompt coloring for PowerShell                                                                                             
1.0.6   ColoredText             PSGallery  The cutting edge API for text coloring and highlighting in powershell.                                                          
1.0     SystemColorsGrid        PSGallery  Shows a pretty grid filled with system colors, their names, hex codes and rgb codes.                                            
1.3.0   PSColorText             PSGallery  Provides a simpler way of writing coloured text to the host.                                                                    
1.0.0   xColors                 PSGallery  Import xColors.net themes to Windows                                                                                            
1.0.0.1 psWriteInformationColor PSGallery  Performs true full-color write-information                                                                                      
0.0.1   ColorMode               PSGallery  Commandlets to control windows color scheme (dark/light)                                                                        
1.0.3   ColorizedHost           PSGallery  PowerShell modules that provides a function to write text to console with specified words highlighted in colors.                
1.0     Write-ColoredOutput     PSGallery  Writes output to pipeline yet capable to give colored output to the screen                                                      
0.0.6   PowerColorLS            PSGallery  List information about files and directories (the current directory by default)....                                             
1.0.0   PSColorizer             PSGallery  Outputs color-formatted messages to console. 
#>

PSWriteColor is the one I'd recommend. See this article about it. Though it is really a wrapper module.

Upvotes: 0

7cc
7cc

Reputation: 1169

Windows 10 + PowerShell 5.1 and later supports ANSI escape sequences by default.

ANSI escape code #DOS and Windows - Wikipedia

and you need to use a special character for [esc] in PowerShell.

# PowerShell version 5
"$([char]0x1b)[30;41m YOUR_TEXT_HERE $([char]0x1b)[0m"

# PowerShell version 6+
"`e[30;41m YOUR_TEXT_HERE `e[0m"

References

Upvotes: 1

Related Questions