24/7/365 Support

Access Features of the Host’s User Interface

Problem

You want to interact with features in the user interface of the hosting application, but PowerShell doesn’t directly provide cmdlets for them.

Solution

To access features of the host’s user interface, use the $host.UI.RawUI variable: $host.UI.RawUI.WindowTitle = (GetLocation)

Discussion

PowerShell itself consists of two main components. The first is an engine that interprets commands, executes pipelines, and performs other similar actions. The second is the hosting application—the way that users interact with the PowerShell engine.

The default shell, PowerShell.exe, is a user interface based on the traditional Windows console. Other applications exist that host PowerShell in a graphical user interface. In fact, PowerShell makes it relatively simple for developers to build their own hosting applications, or even to embed the PowerShell engine features into their own application.

You (and your scripts) can always depend on the functionality available through the $host.UI variable, as that functionality remains the same for all hosts. Example 127 shows the features available to you in all hosts.

Example 127. Functionality available through the $host.UI property

PS >$host.UI | GetMember | Select Name,MemberType | FormatTable Auto

Name
MemberType

(...)

Prompt
Method

PromptForChoice
Method

PromptForCredential
Method

ReadLine
Method

ReadLineAsSecureString Method Write Method WriteDebugLine Method WriteErrorLine Method WriteLine Method WriteProgress Method WriteVerboseLine Method WriteWarningLine Method RawUI Property

If you (or your scripts) want to interact with portions of the user interface specific to the current host, PowerShell provides that access through the $host.UI.RawUI variable. Example 128 shows the features available to you in the PowerShell console host.

Example 128. Functionality available through the default console host

PS >$host.UI.RawUI | GetMember | >> Select Name,MemberType | FormatTable Auto >>

Name MemberType

(...) FlushInputBuffer Method GetBufferContents Method

Example 128. Functionality available through the default console host (continued)

GetHashCode
Method

GetType
Method

LengthInBufferCells
Method

NewBufferCellArray
Method

ReadKey
Method

ScrollBufferContents
Method

SetBufferContents
Method

BackgroundColor
Property

BufferSize
Property

CursorPosition
Property

CursorSize
Property

ForegroundColor
Property

KeyAvailable
Property

MaxPhysicalWindowSize
Property

MaxWindowSize
Property

WindowPosition
Property

WindowSize
Property

WindowTitle
Property

If you rely on the hostspecific features from $host.UI.RawUI, be aware that your script will require modifications (perhaps major) before it will run properly on other hosts.

Help Category:

Get Windows Dedicated Server

Only reading will not help you, you have to practice it! So get it now.

Processor RAM Storage Server Detail
Intel Atom C2350 1.7 GHz 2c/2t 4 GB DDR3 1× 1 TB (HDD SATA) Configure Server
Intel Atom C2350 1.7 GHz 2c/2t 4 GB DDR3 1× 128 GB (SSD SATA) Configure Server
Intel Atom C2750 2.4 GHz 8c/8t 8 GB DDR3 1× 1 TB (HDD SATA) Configure Server
Intel Xeon E3-1230 v2 3.3 GHz 4c/8t 16 GB DDR3 1× 256 GB (SSD SATA) Configure Server
Intel Atom C2350 1.7 GHz 2c/2t 4 GB DDR3 1× 250 GB (SSD SATA) Configure Server

What Our Clients Say