Windows PowerShell Properties: Describes what the object is _powershell

Source: Internet
Author: User
Tags constructor

Attributes can describe an object, and the object's properties can be automatically converted to text by PowerShell and exported to the console. So you can view any object in this way, such as $host:

Copy Code code as follows:

PS c:powershell> $host

Name:consolehost
version:2.0
Instanceid:7fefa1fa-fb2e-47c7-a867-c13b123da5c2
UI:System.Management.Automation.Internal.Host.InternalHostUserInterface
Currentculture:zh-cn
Currentuiculture:zh-cn
Privatedata:microsoft.powershell.consolehost+consolecolorproxy
Isrunspacepushed:false
Runspace:System.Management.Automation.Runspaces.LocalRunspace

The Internalhost object is stored in the $host variable and contains 9 properties. The first column of the output is the property of the object, and the second column is the property value in the text form. For example, to view the version number of the current PowerShell, you can access the versions property of the $host object:

Copy Code code as follows:

PS c:powershell> $host. Version

Major Minor Build Revision
-----  -----  -----  --------
2 0-1-1

It follows that version is not stored as a separate number, but is itself an object that contains the Major,minor,build,revision four properties, can view the specific type of version, or access each of its properties:

Copy Code code as follows:

PS c:powershell> $Host. Version.gettype (). FullName
System.version
PS c:powershell> $Host. Version.build
-1
PS c:powershell> $Host. version.major
2
PS c:powershell> $Host. version.majorrevision
-1
PS c:powershell> $Host. version.revision
-1

It is useful to see the type of an object, because you can construct new objects or type conversions from this type, and so on.

Copy Code code as follows:

PS c:powershell> [system.version] ' 2012.12.20.4444 '

Major Minor Build Revision
-----  -----  -----  --------
2012 12 20 4444

For example, the CurrentCulture property allows you to access the current system's localized information and the type of information through $host CurrentCulture:

Copy Code code as follows:

PS c:powershell> $Host. CurrentCulture

LCID Name DisplayName
----             ----             -----------
2052 ZH-CN Chinese (People's Republic of China)

PS c:powershell> $Host. Currentculture.gettype (). FullName
System.Globalization.CultureInfo

CurrentCulture contains 3 attributes, LCID, Name, and DisplayName. Using MSDN to see the System.Globalization.CultureInfo constructor, you can convert country code and country name flag strings to a new CultureInfo object.

Copy Code code as follows:

PS c:powershell> [System.Globalization.CultureInfo] ' ZH-CN '

LCID Name DisplayName
----             ----             -----------
2052 ZH-CN Chinese (People's Republic of China)

PS c:powershell> [System.Globalization.CultureInfo] ' ZH-TW '

LCID Name DisplayName
----             ----             -----------
1028 ZH-TW Chinese (Taiwan)

PS c:powershell> [System.Globalization.CultureInfo] ' en-US '

LCID Name DisplayName
----             ----             -----------
1033 en-US English (USA)

PS c:powershell> [System.Globalization.CultureInfo] 55

LCID Name DisplayName
----             ----             -----------
The Ka-Georgian language

PS c:powershell> [System.Globalization.CultureInfo] 1

LCID Name DisplayName
----             ----             -----------
1 ar Arabic

PS c:powershell> [System.Globalization.CultureInfo] 33

LCID Name DisplayName
----             ----             -----------
ID Indonesian

property contains the object

An object's properties are used to store the data, which in turn can store other objects. The $host has two more specific property UIs and Privatedata. After the $host object is exported to the console, all properties except the UI and Privatedata are converted to the defined text:

Copy Code code as follows:

PS c:powershell> $Host

Name:consolehost
version:2.0
Instanceid:7fefa1fa-fb2e-47c7-a867-c13b123da5c2
UI:System.Management.Automation.Internal.Host.InternalHostUserInterface
Currentculture:zh-cn
Currentuiculture:zh-cn
Privatedata:microsoft.powershell.consolehost+consolecolorproxy
Isrunspacepushed:false
Runspace:System.Management.Automation.Runspaces.LocalRunspace

The reason is that the two properties also contain an object:

Copy Code code as follows:

PS c:powershell> $Host. UI

Rawui
-----
System.Management.Automation.Internal.Host.InternalHostRawUserInterface

PS c:powershell> $Host. Ui.rawui

Foregroundcolor:darkyellow
Backgroundcolor:darkmagenta
cursorposition:0,23
windowposition:0,0
Cursorsize:25
buffersize:100,200
windowsize:100,61
maxwindowsize:100,62
maxphysicalwindowsize:160,62
Keyavailable:false
Windowtitle:windows PowerShell

"Rawui" provides an interface for configuring the PowerShell console user interface for RAW user Interface. The above properties can be read, but individual cannot be changed.

Read-only properties and read-write properties

Property can accurately describe the object once the property has changed. This change will also be reflected on the object. If it cannot be changed, the property is the read-only property.
By simply modifying the color of the console's background and foreground, you can see that property changes can be directly reflected on the object.

Copy Code code as follows:

PS c:powershell> $host. Ui.rawui.BackgroundColor = "Green"
PS c:powershell> $host. Ui.rawui.ForegroundColor = "White"
PS c:powershell> CLS

Some properties cannot be changed, and if you try to modify them, an exception is thrown.

Copy Code code as follows:

PS c:powershell> $Host. UI.RawUI.KeyAvailable
False
PS c:powershell> $Host. ui.rawui.keyavailable= $false

"Keyavailable" is the ReadOnly property.

Location: 1 Characters: 16
+ $Host. Ui.rawui. <<<< keyavailable= $false
+ categoryinfo:invalidoperation: (:) [], RuntimeException
+ fullyqualifiederrorid:propertyassignmentexception

If the console receives a key request, it should depend on the user's action, so the property refuses to be changed and you can only read it.

Properties of Rawui

Foregroundcolor: Foreground color
BackgroundColor: Background color
Cursorposition: Position of the cursor
Windowposition: The location of the window
Cursorsize: The size of the cursor
BufferSize: Size of buffer
WindowSize: Size of window
Maxwindowsize: Allows maximum window size
Maxphysicalwindowsize: The maximum possible value for a window
Keyavailable: Key is present
WindowTitle: Title of Window

Type of property

Some properties accept only integer values, such as the size of the console cursor and the range of 0-100, to control the percentage of the shutdown size. You can set the cursor to 75%, but not more than 100%, or you will generate an error.

Copy Code code as follows:

PS c:powershell> $Host. ui.rawui.cursorsize=75
PS c:powershell> $Host. ui.rawui.cursorsize=101

An exception occurred while setting ' Cursorsize: ' Unable to process cursorsize because the specified cursor size is invalid.
Parameter name: value
The actual value is 101. ”
Location: 1 Characters: 16

Copy Code code as follows:

+ $Host. Ui.rawui. <<<< cursorsize=101
+ categoryinfo:invalidoperation: (:) [], RuntimeException
+ fullyqualifiederrorid:propertyassignmentexception

The type of another property Foregoundcolor is a color enumeration value. Therefore, the value assigned to Foregoundcolor must be defined already in the System.consolecolor. You can "black" but you can't use "Pink."

Copy Code code as follows:

PS c:powershell> $Host. ui.rawui.foregroundcolor= "BLACK"
PS c:powershell> $Host. ui.rawui.foregroundcolor= "Pink"
An exception occurred while setting ' Foregroundcolor: ' The value ' Pink ' cannot be converted to type ' because the enumeration value is invalid System.consolecolor
”。 Please specify one of the following enumeration values and try again. The possible enumeration values are black, Darkblue, Darkgreen, Darkcyan, darkred,
Darkmagenta, Darkyellow, Gray, Darkgray, Blue, Green, cyan, Red, magenta, yellow, white. ”
Location: 1 Characters: 16
+ $Host. Ui.rawui. <<<< foregroundcolor= "Pink"
+ categoryinfo:invalidoperation: (:) [], RuntimeException
+ fullyqualifiederrorid:propertyassignmentexception

You can use the System.enum]::getnames method to view all colors defined by Consolecolor.

Copy Code code as follows:

PS c:powershell> [System.enum]::getnames ([System.consolecolor])
Black
Darkblue
Darkgreen
Darkcyan
Darkred
Darkmagenta
Darkyellow
Gray
Darkgray
Blue
Green
Cyan
Red
Magenta
Yellow
White

Sometimes a property expects an assignment that must be an object of a specified type. For example WindowSize, if you want to change the window size of the PowerShell, but set the WindowSize property, but it is a System.Management.Automation.Host.Size object, how to get the object?
1. Read the attribute first, save as a temporary variable, change the temporary variable, and assign the temporary variable to windowsize
2. Create a System.Management.Automation.Host.Size directly and assign it to windowsize

Copy Code code as follows:

PS c:powershell> $tmp = $Host. UI.RawUI.WindowSize
PS c:powershell> $tmp

Width Height
----- ------
100 60

PS c:powershell> $tmp. Height=30
PS c:powershell> $tmp. Width=60
PS c:powershell> $Host. ui.rawui.windowsize= $tmp
Width Height
----- ------
60 30

PS c:powershell> $Host. Ui.rawui.windowsize=new-object System.Management.Automation.Host.Size (60,40)
PS c:powershell> $Host. UI.RawUI.WindowSize

Width Height
----- ------
60 40

View All Properties

Because both properties and methods are members of an object, you can use Get-member to return details about their members, and if you want to display only properties, you can use the parameter MemberType as the property

Copy Code code as follows:

PS c:powershell> $host | Get-member-membertype Property

TypeName:System.Management.Automation.Internal.Host.InternalHost

Name MemberType Definition
----             ---------- ----------
CurrentCulture Property System.Globalization.CultureInfo CurrentCulture {get;}
CurrentUICulture Property System.Globalization.CultureInfo CurrentUICulture {get;}
Instanceid Property System.Guid Instanceid {get;}
Isrunspacepushed Property System.Boolean isrunspacepushed {get;}
Name Property System.String name {get;}
Privatedata Property System.Management.Automation.PSObject privatedata {get;}
Runspace Property System.Management.Automation.Runspaces.Runspace runspace {get;}
UI property System.Management.Automation.Host.PSHostUserInterface UI {get;}
Version Property system.version version {get;}

In the Name column, you can see all the properties supported by $host. The definition column lists the specific types of the properties first, and then lists the constructors, if there is only a Get method in one constructor, and no set method, which indicates that the property is read-only.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.