Muokkaa

Jaa


About intrinsic members

Short description

Provides information about PowerShell's intrinsic members that are available to all PowerShell objects.

Detailed description

When objects are created, PowerShell adds some "hidden" properties and methods to each object. These properties and methods are known as intrinsic members. These intrinsic members are normally hidden from view. These hidden members can be seen using the Force parameter of Get-Member.

Object views

The intrinsic members include a set of MemberSet properties that represent a view of the object. For more information about MemberSet properties, see PSMemberSet.

Every PowerShell object includes the following properties.

  • psbase

    The psbase MemberSet contains the members of the base object without extension or adaptation. Depending on the object type it's either a .NET instance wrapped by a [psobject] instance or, if there's no wrapper, it's the input object itself.

  • psadapted

    The psadapted MemberSet shows the base object plus the adapted members, if present. Adapted members are added by the Extended Type System (ETS).

  • psextended

    The psextended MemberSet only shows the members added by the Types.ps1xml files and the Add-Member cmdlet. Any object can be extended at runtime using the Add-Member cmdlet.

  • psobject

    The psobject MemberSet a rich source of reflection for any object that includes methods, properties, and other information about the object.

Examples

For this example, $hash is a hashtable containing information about a user. The Force parameter of Get-Member shows us the intrinsic members of the object.

$hash = @{
    Age  = 33
    Name = 'Bob'
}

$hash | Get-Member -Force -MemberType MemberSet, CodeProperty
   TypeName: System.Collections.Hashtable

Name        MemberType   Definition
----        ----------   ----------
pstypenames CodeProperty System.Collections.ObjectModel.Collection`1[[System.String, System.Private.CoreLib, Version=7…
psadapted   MemberSet    psadapted {Item, IsReadOnly, IsFixedSize, IsSynchronized, Keys, Values, SyncRoot, Count, Add,…
psbase      MemberSet    psbase {Item, IsReadOnly, IsFixedSize, IsSynchronized, Keys, Values, SyncRoot, Count, Add, Cl…
psextended  MemberSet    psextended {}
psobject    MemberSet    psobject {Members, Properties, Methods, ImmediateBaseObject, BaseObject, TypeNames, get_Membe…

Using psobject is similar to using Get-Member, but provides more flexibility. For example, you can enumerate the properties of an object and their values.

$hash.psobject.Properties | Select-Object Name, MemberType, Value
Name           MemberType                    Value
----           ----------                    -----
IsReadOnly       Property                    False
IsFixedSize      Property                    False
IsSynchronized   Property                    False
Keys             Property              {Age, Name}
Values           Property                {33, Bob}
SyncRoot         Property {[Age, 33], [Name, Bob]}
Count            Property                        2

Compare that to the object created by converting the hashtable to a PSCustomObject.

$user = [pscustomobject]$hash
$user.psobject.Properties | Select-Object Name, MemberType, Value
Name   MemberType Value
----   ---------- -----
Age  NoteProperty    33
Name NoteProperty   Bob

Notice that the keys from the hashtable have been converted to properties in the PSCustomObject. The new properties are now part of the psextended MemberSet.

$user | Get-Member -Force -MemberType MemberSet, CodeProperty
   TypeName: System.Management.Automation.PSCustomObject

Name        MemberType   Definition
----        ----------   ----------
pstypenames CodeProperty System.Collections.ObjectModel.Collection`1[[System.String, System.Private.CoreLib, Version=7…
psadapted   MemberSet    psadapted {ToString, GetType, Equals, GetHashCode}
psbase      MemberSet    psbase {ToString, GetType, Equals, GetHashCode}
psextended  MemberSet    psextended {Age, Name}
psobject    MemberSet    psobject {Members, Properties, Methods, ImmediateBaseObject, BaseObject, TypeNames, get_Membe…

Type information

The pstypenames CodeProperty lists the object type hierarchy in order of inheritance. For example:

$file = Get-Item C:\temp\test.txt
$file.pstypenames
System.IO.FileInfo
System.IO.FileSystemInfo
System.MarshalByRefObject
System.Object

The Output starts with the most specific object type, System.IO.FileInfo, and continues down to the most generic type, System.Object.

Methods

PowerShell adds two hidden methods to all PowerShell objects. These methods are not visible using the Get-Member -Force command or tab completion.

ForEach() and Where()

The ForEach() and Where() methods are available to all PowerShell objects. However, they're most useful when working with collections. For more information on how to use these methods, see about_Arrays.

Properties

The Count and Length properties are available to all PowerShell objects, not just collections. These are similar to each other but may work differently depending on the data type. For example, the Length of a string is the number of characters in the string. The Count property is the number of instances of the object.

PS> $str = 'string'
PS> $str.Length
6
PS> $str.Count
1

For more information about these properties, see about_Properties.

Array indexing scalar types

When an object isn't an indexed collection, using the index operator to access the first element returns the object itself. Index values beyond the first element return $null.

PS> (2)[0]
2
PS> (2)[-1]
2
PS> (2)[1] -eq $null
True
PS> (2)[0,0] -eq $null
True

For more information, see about_Operators.

New() method for types

Beginning in PowerShell 5.0, PowerShell adds a static New() method for all .NET types. The following examples produce the same result.

$expression = New-Object -TypeName regex -ArgumentList 'pattern'
$expression = [regex]::new('pattern')

Using the new() method performs better than using New-Object.

For more information, see about_Classes.