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.