PowerShell equivalent to DOS Color and Mode commands.

Copper Contributor

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 PowerShell so that the user interface stays the same. 

 

One of the DOS commands that I uses a lot is Color.  When you change the foreground and background  color with DOS, the entire DOS windows changes, which is what I want to do with PowerShell.  I do this to indicate to the user that attention is needed.  For instance, I use a white background to indicate to the user that the script is running.  I will use a yellow background to indicate to the user that dialog is required.  And I use red to indicate that there is an error that needs attention.  I cannot figure out how to get PowerShell to do the DOS equivalent.

 

I use the DOS Mode command in all of my scripts to set the window size.  This is very important so that I know that the user can see all of the relevant data.  I usually set the window to 80 or 128 characters wide, and 48 or 64 characters long.  I cannot figure out how to get PowerShell to do this.

 

I also need to figure out how to set the window location.  I do this with DOS using a registry hack that I created.  The hack records the user settings, changes them as needed, and then sets them back for the user.  I need to figure out how to have PowerShell display multiple windows in specific locations.

 

Finally, need to figure out how to set the font for the display.  I usually set the display to Lucida Console.

 

I have tried searching online for how to do this.  Most examples are not "in script", so it is hard to find them.  I need help with this as well.

 

2 Replies

@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