获取表示当前主机程序的对象。默认情况下,还显示 Windows PowerShell 的版本和区域信息。

语法

Get-Host [<CommonParameters>]

说明

Get-Host cmdlet 获取表示托管 Windows PowerShell 的程序的对象。

默认显示内容包括 Windows PowerShell 版本号以及主机使用的当前区域和语言设置,但是主机对象包含大量信息,其中包括有关当前正在运行的 Windows PowerShell 版本以及 Windows PowerShell 的当前区域性和 UI 区域性的详细信息。也可使用该 cmdlet 对主机程序用户界面的特性(如文本和背景色)进行自定义。

参数

<CommonParameters>

此 cmdlet 支持通用参数:-Verbose、-Debug、-ErrorAction、-ErrorVariable、-OutBuffer 和 -OutVariable。有关详细信息,请参阅 about_Commonparameters.

输入和输出

输入类型是指可通过管道传递给 cmdlet 的对象的类型。返回类型是指 Cmdlet 所返回对象的类型。

输入

None

不能通过管道将输入传递给此 cmdlet。

输出

System.Management.Automation.Internal.Host.InternalHost

Get-Host 返回一个 System.Management.Automation.Internal.Host.InternalHost 对象。

说明

$host 自动变量包含的对象与 Get-Host 返回的对象相同,您可采用相同的方式来使用它。同样,$PSCulture 和 $PSUICulture 自动变量包含的对象与主机对象的 CurrentCulture 和 CurrentUICulture 属性包含的对象相同。可交替使用这些功能。

有关详细信息,请参阅 about_Automatic_Variables。

示例 1

C:\PS>get-host

Name             : ConsoleHost
Version          : 2.0
InstanceId       : e4e0ab54-cc5e-4261-9117-4081f20ce7a2
UI               : System.Management.Automation.Internal.Host.InternalHostUserInterface
CurrentCulture   : en-US
CurrentUICulture : en-US
PrivateData      : Microsoft.PowerShell.ConsoleHost+ConsoleColorProxy
IsRunspacePushed : False
Runspace         : System.Management.Automation.Runspaces.LocalRunspace

说明
-----------
此命令显示有关 Windows PowerShell 控制台的信息,在本示例中,该控制台是 Windows PowerShell 的当前主机程序。它包括主机的名称、主机中正在运行的 Windows PowerShell 版本,以及当前区域性和 UI 区域性。

Version、UI、CurrentCulture、CurrentUICulture、PrivateData 和 Runspace 属性都包含一个对象,且该对象都具有一些非常有用的属性。后面的示例将检查这些属性。






示例 2

C:\PS>$h = get-host

C:\PS> $win = $h.ui.rawui.windowsize

C:\PS> $win.height = 10

C:\PS> $win.width  = 10

C:\PS> $h.ui.rawui.set_windowsize($win)

说明
-----------
此命令将 Windows PowerShell 窗口重新调整为 10 x 10 像素。






示例 3

C:\PS>(get-host).version | format-list -property *

Major         : 2
Minor         : 0
Build         : -1
Revision      : -1
MajorRevision : -1
MinorRevision : -1

说明
-----------
此命令获取有关主机中运行的 Windows PowerShell 版本的详细信息。您可以查看这些值,但不能更改它们。

Get-Host 的 Version 属性包含 System.Version 对象。此命令使用管道运算符 (|) 将版本对象发送至 Format-List cmdlet。Format-List 命令使用值为 *(全部)的 Property 参数来显示版本对象的所有属性和属性值。






示例 4

C:\PS>(get-host).currentculture | format-list -property *

Parent                         : en
LCID                           : 1033
KeyboardLayoutId               : 1033
Name                           : en-US
IetfLanguageTag                : en-US
DisplayName                    : English (United States)
NativeName                     : English (United States)
EnglishName                    : English (United States)
TwoLetterISOLanguageName       : en
ThreeLetterISOLanguageName     : eng
ThreeLetterWindowsLanguageName : ENU
CompareInfo                    : CompareInfo - 1033
TextInfo                       : TextInfo - 1033
IsNeutralCulture               : False
CultureTypes                   : SpecificCultures, InstalledWin32Cultures, FrameworkCultures
NumberFormat                   : System.Globalization.NumberFormatInfo
DateTimeFormat                 : System.Globalization.DateTimeFormatInfo
Calendar                       : System.Globalization.GregorianCalendar
OptionalCalendars              : {System.Globalization.GregorianCalendar, System.Globalization.GregorianCalendar}
UseUserOverride                : True
IsReadOnly                     : False

说明
-----------
此命令获取有关主机中运行的 Windows PowerShell 的当前区域性设置的详细信息。该信息与 Get-Culture cmdlet 返回的信息相同。

(同样,CurrentUICulture 属性返回的对象与 Get-UICulture 返回的对象相同。)

主机对象的 CurrentCulture 属性包含 System.Globalization.CultureInfo 对象。此命令使用管道运算符 (|) 将 CultureInfo 对象发送至 Format-List cmdlet。Format-List 命令使用值为 *(全部)的 Property 参数来显示 CultureInfo 对象的所有属性和属性值。






示例 5

C:\PS>(get-host).currentculture.DateTimeFormat | format-list -property *

AMDesignator                     : AM
Calendar                         : System.Globalization.GregorianCalendar
DateSeparator                    : /
FirstDayOfWeek                   : Sunday
CalendarWeekRule                 : FirstDay
FullDateTimePattern              : dddd, MMMM dd, yyyy h:mm:ss tt
LongDatePattern                  : dddd, MMMM dd, yyyy
LongTimePattern                  : h:mm:ss tt
MonthDayPattern                  : MMMM dd
PMDesignator                     : PM
RFC1123Pattern                   : ddd, dd MMM yyyy HH':'mm':'ss 'GMT'
ShortDatePattern                 : M/d/yyyy
ShortTimePattern                 : h:mm tt
SortableDateTimePattern          : yyyy'-'MM'-'dd'T'HH':'mm':'ss
TimeSeparator                    : :
UniversalSortableDateTimePattern : yyyy'-'MM'-'dd HH':'mm':'ss'Z'
YearMonthPattern                 : MMMM, yyyy
AbbreviatedDayNames              : {Sun, Mon, Tue, Wed...}
ShortestDayNames                 : {Su, Mo, Tu, We...}
DayNames                         : {Sunday, Monday, Tuesday, Wednesday...}
AbbreviatedMonthNames            : {Jan, Feb, Mar, Apr...}
MonthNames                       : {January, February, March, April...}
IsReadOnly                       : False
NativeCalendarName               : Gregorian Calendar
AbbreviatedMonthGenitiveNames    : {Jan, Feb, Mar, Apr...}
MonthGenitiveNames               : {January, February, March, April...}

说明
-----------
此命令返回有关 Windows PowerShell 使用的当前区域性的 DateTimeFormat 的详细信息。

主机对象的 CurrentCulture 属性包含 CultureInfo 对象,而 CultureInfo 对象具有许多有用的属性。其中,DateTimeFormat 属性包含 DateTimeFormatInfo 对象,而该对象又具有许多有用的属性。

要查找存储在对象属性中的对象的类型,请使用 Get-Member cmdlet。要显示对象的属性值,请使用 Format-List cmdlet。






示例 6

C:\PS>(get-host).ui.rawui | format-list -property *

ForegroundColor       : DarkYellow
BackgroundColor       : DarkBlue
CursorPosition        : 0,390
WindowPosition        : 0,341
CursorSize            : 25
BufferSize            : 120,3000
WindowSize            : 120,50
MaxWindowSize         : 120,81
MaxPhysicalWindowSize : 182,81
KeyAvailable          : False
WindowTitle           : Windows PowerShell 2.0 (04/11/2008 00:08:14)

说明
-----------
此命令显示主机对象的 RawUI 属性的各属性。通过更改这些值,可以更改主机程序的外观。






示例 7

C:\PS>(get-host).ui.rawui.backgroundcolor = "Black"

C:\PS> cls

说明
-----------
这些命令将 Windows PowerShell 控制台的背景色更改为黑色。“cls”命令是 Clear-Host 函数的别名,该函数可清除屏幕并将整个屏幕更改为新的颜色。

此更改仅在当前会话中有效。若要更改所有会话的控制台背景色,请将该命令添加到 Windows PowerShell 配置文件中。






示例 8

C:\PS>$host.privatedata.errorbackgroundcolor = "white"

说明
-----------
此命令将错误消息的背景色更改为白色。

此命令使用自动变量 $host,该变量包含当前主机程序的主机对象。Get-Host 会返回 $host 包含的同一对象,所以它们可以互换。

此命令使用 $host 的 PrivateData 属性作为其 ErrorBackgroundColor 属性。若要查看 $host.privatedata 属性中对象的所有属性,请键入“$host.privatedata | format-list *”。






另请参阅




目录