Use .NET Classes to Configure the Windows PowerShell Console

ScriptingGuy1

 

Summary: The Microsoft Scripting Guys show how to use .NET Framework classes to configure the Windows PowerShell console.

 

Hey, Scripting Guy! QuestionHey, Scripting Guy! I am curious about how the .NET Framework classes work. I saw posts from a couple weeks ago, but I still have questions. For example, it seems that the Windows PowerShell console is basically an old fashioned DOS prompt. How would the .NET Framework apply to that?

— TL

 

Hey, Scripting Guy! AnswerHello TL, Microsoft Scripting Guy Ed Wilson here. It is humid and tepid outside. It is the kind of weather that reminds me of winter when I was growing up in Florida. It is, of course still considered fall in the South Eastern part of the United States; winter does not officially begin down here until December 22. So maybe the weather reminds me of fall when I was growing up in Florida. At any rate, all I need is the smell of Palm trees, Coconut oil, and salt spray and I will be back home. I have the windows open in my office, and three fans going … I refuse to turn on the air conditioner in November just on general principles! Maybe I will turn on some Beach Boys on my Zune HD and just go with the flow. Now, if I only had some pink lemonade I would be all set.

TL, there is a .NET Framework class named Console that resides in the System namespace. The System.Console .NET Framework class represents the input, output and error streams for console applications. The Windows PowerShell console is a console application. According to MSDN, all the members (properties, methods, and events) of the Console class are static. MSDN represents static members with a big red “s” as shown in the following figure.

 

The cool thing about static members is that they are very easy to discover in Windows PowerShell. To do this I use the Get-Member Windows PowerShell cmdlet. Because it is Windows PowerShell, I can format the output as I want by piping the output to additional format cmdlets. In the example shown here, I pipeline the output to the Format-Table cmdlet and select only the name and definition properties.

PS C:\> [system.console] | Get-Member -Static -MemberType property | Format-Table nam
e, definition -AutoSize

Name                 Definition
—-                 ———-
BackgroundColor      static System.ConsoleColor BackgroundColor {get;set;}
BufferHeight         static System.Int32 BufferHeight {get;set;}
BufferWidth          static System.Int32 BufferWidth {get;set;}
CapsLock             static System.Boolean CapsLock {get;}
CursorLeft           static System.Int32 CursorLeft {get;set;}
CursorSize           static System.Int32 CursorSize {get;set;}
CursorTop            static System.Int32 CursorTop {get;set;}
CursorVisible        static System.Boolean CursorVisible {get;set;}
Error                static System.IO.TextWriter Error {get;}
ForegroundColor      static System.ConsoleColor ForegroundColor {get;set;}
In                   static System.IO.TextReader In {get;}
InputEncoding        static System.Text.Encoding InputEncoding {get;set;}
KeyAvailable         static System.Boolean KeyAvailable {get;}
LargestWindowHeight  static System.Int32 LargestWindowHeight {get;}
LargestWindowWidth   static System.Int32 LargestWindowWidth {get;}
NumberLock           static System.Boolean NumberLock {get;}
Out                  static System.IO.TextWriter Out {get;}
OutputEncoding       static System.Text.Encoding OutputEncoding {get;set;}
Title                static System.String Title {get;set;}
TreatControlCAsInput static System.Boolean TreatControlCAsInput {get;set;}
WindowHeight         static System.Int32 WindowHeight {get;set;}
WindowLeft           static System.Int32 WindowLeft {get;set;}
WindowTop            static System.Int32 WindowTop {get;set;}
WindowWidth          static System.Int32 WindowWidth {get;set;}

 

The System.Console .NET Framework class also contains several static methods. The methods are shown here.

PS C:\> [system.console] | Get-Member -Static -MemberType method | Format-Table name,

 definition -AutoSize

 

Name               Definition

—-               ———-

Beep               static System.Void Beep(), static System.Void Beep(int frequen…

Clear              static System.Void Clear()

Equals             static bool Equals(System.Object objA, System.Object objB)

MoveBufferArea     static System.Void MoveBufferArea(int sourceLeft, int sourceTo…

OpenStandardError  static System.IO.Stream OpenStandardError(), static System.IO….

OpenStandardInput  static System.IO.Stream OpenStandardInput(), static System.IO….

OpenStandardOutput static System.IO.Stream OpenStandardOutput(), static System.IO…

Read               static int Read()

ReadKey            static System.ConsoleKeyInfo ReadKey(), static System.ConsoleK…

ReadLine           static string ReadLine()

ReferenceEquals    static bool ReferenceEquals(System.Object objA, System.Object …

ResetColor         static System.Void ResetColor()

SetBufferSize      static System.Void SetBufferSize(int width, int height)

SetCursorPosition  static System.Void SetCursorPosition(int left, int top)

SetError           static System.Void SetError(System.IO.TextWriter newError)

SetIn              static System.Void SetIn(System.IO.TextReader newIn)

SetOut             static System.Void SetOut(System.IO.TextWriter newOut)

SetWindowPosition  static System.Void SetWindowPosition(int left, int top)

SetWindowSize      static System.Void SetWindowSize(int width, int height)

Write              static System.Void Write(string format, System.Object arg0), s…

WriteLine          static System.Void WriteLine(), static System.Void WriteLine(b…

 

 

PS C:\>

 

The Console class has several static properties and methods that let you discover current Windows PowerShell console settings. These include the WindowHeight, WindowWidth, BackgroundColor, ForegroundColor, and Title properties. The code to retrieve these settings is seen here.

PS C:\> [console]::WindowHeight
10
PS C:\> [console]::windowWidth
47
PS C:\> [console]::BackgroundColor
DarkGray
PS C:\> [console]::ForegroundColor
White
PS C:\> [console]::Title
scripting guys rock!
PS C:\>

 

These are seen in the following figure.

 

 

The properties are read/write. Therefore I can assign new values to them to change the way the Windows PowerShell console is displayed. The code to do this is seen here.

PS C:\> [console]::WindowHeight = 14

PS C:\> [console]::WindowWidth = 50

PS C:\> [console]::BackgroundColor = “blue”

PS C:\> [console]::ForegroundColor = “yellow”

PS C:\> [console]::Title = “a new title”

PS C:\>

 

The changes occur immediately. As seen in following figure the background and foreground colors do not affect the whole Windows PowerShell console, only the line where the code has executed, and new lines that are created. You can force the changes to paint the whole console by using the clear-host command.

 

After you close the Windows PowerShell console, the changes revert to the values that are specified for the console itself. You can cause your customizations to be applied every time that the Windows PowerShell console is opened by adding the commands to your Windows PowerShell console profile.

Instead of assigning the height and width value to the window size, you can use the SetWindowSize method instead. It accepts the width and the height parameters as parameters to the static method. This is seen here.

PS C:\> [console]::SetWindowSize(45,7)

PS C:\>

 

When the command is run, the Windows PowerShell console will resize itself as seen in the following figure.

 

 

To discover what kind of enumeration values can be used for the Windows PowerShell console background colors and foreground colors, I use the Get-EnumAndValues.ps1 script I developed for the Hey, Scripting Guy! Weekend Scripter: Enumerations and Values post I wrote back during the summer. In fact, this particular function, Get-EnumValues is so useful; I have added it to my Windows PowerShell Profile. As seen in the following figure there are 16 colors defined for the System.ConsoleColor enumeration and the values range from 0 – 15.

 

 

The cool thing about finding the enumeration names and values is that it makes it possible to display all the colors to see what would work best. Before playing around too much with the Windows PowerShell console colors, I like to know what the command is to reset the colors back to their default values. It is the resetcolors static method. Here is that command.

[console]::ResetColor()

 

Here is the code that I used to display the 16 background colors. The % sign is an alias for the Foreach-Object Windows PowerShell cmdlet.

0..15 | % { [console]::BackgroundColor = $_ ; “console color $_” ; sleep 1 }

 

When I run this command, the output seen in the following figure appears. The last line, that cannot be read because it is basically whited out is the [console]::ResetColor() command.

 

 

To see how well font colors are displayed, I change the code to change foreground colors. The modified code is seen here.

0..15 | % { [console]::ForegroundColor = $_ ; “console color $_” ; sleep 1 }

 

When I run this command, the output seen in the following figure appears.

 

 

TL, that is all there is to using the System.Console .NET Framework class to configure the Windows PowerShell console. .NET Framework week will continue tomorrow when I will talk about how to work with static methods on .NET Framework classes.

I invite you to follow me on Twitter or Facebook. If you have any questions, send email to me at scripter@microsoft.com or post them on the Official Scripting Guys Forum.. See you tomorrow. Until then, peace.

 

Ed Wilson, Microsoft Scripting Guy

0 comments

Discussion is closed.

Feedback usabilla icon