Forum Discussion
MoneyTree
Dec 03, 2021Copper Contributor
PowerShell equivalent to DOS Color and Mode commands.
I am in the middle of manually converting all my library of DOS batch scripts over to Windows PowerShell (v5). I manipulate the DOS window a lot in my scripts, and I need to do the same with PowerSh...
SteveMacNZ
Jan 12, 2022Iron Contributor
MoneyTree I use a combination of hash table and write-host for this inside of the script for controlling output colour - Harm links will allow for the windows to be changed - I used this as depending on what PS console you use there can be different colouring (e.g. standard PS console version Exchange PS Console)
#--------------------------------------[Hash Tables]------------------------------------
#* Hash table for Write-Host Errors to be used as spatted
$cerror = @{ForeGroundColor = "Red"; BackgroundColor = "white"}
#* Hash table for Write-Host Warnings to be used as spatted
$cwarning = @{ForeGroundColor = "Magenta"; BackgroundColor = "white"}
#* Hash table for Write-Host highlighted to be used as spatted
$chighlight = @{ForeGroundColor = "Blue"; BackgroundColor = "white"}
Write-Host "This is standard output" #Standard output
Write-Host "This is a warning message" @cwarning
Write-Host "This is a highlighted message" @chighlight
Write-Host "This is a error message" @cerror
you can also just use the -ForeGroundColor and -BackgroundColor switches at the end of the write-host as well.
Note this only works with the Write-Host output, using other Write-* methods do not support these switches