关于属性

简短说明

介绍如何在 PowerShell 中使用对象属性。

长说明

PowerShell 使用称为 对象的结构化信息集合来表示数据存储中的项或计算机的状态。 通常,你使用属于 Microsoft .NET Framework的对象,但也可以在 PowerShell 中创建自定义对象。

项与其对象之间的关联非常接近。 更改对象时,通常会更改它所表示的项。 例如,在 PowerShell 中获取文件时,不会获取实际文件。 而是获取一个代表该文件的 FileInfo 对象。 更改 FileInfo 对象时,文件也会更改。

大多数对象都有属性。 属性是与对象关联的数据。 不同类型的对象具有不同的属性。 例如,表示文件的 FileInfo 对象具有 IsReadOnly 属性,该属性包含$True如果文件为只读属性,则$False(否则)。 表示文件系统目录的 DirectoryInfo 对象具有 Parent 属性,该属性包含父目录的路径。

对象属性

若要获取对象的属性,请使用 Get-Member cmdlet。 例如,若要获取 FileInfo 对象的属性,请使用 Get-ChildItem cmdlet 获取表示文件的 FileInfo 对象。 然后,使用管道运算符 (|) 将 FileInfo 对象发送到 Get-Member。 以下命令获取 PowerShell.exe 文件并将其发送到 Get-Member。 $Pshome自动变量包含 PowerShell 安装目录的路径。

Get-ChildItem $pshome\PowerShell.exe | Get-Member

命令的输出列出了 FileInfo 对象的成员。 成员包括属性和方法。 在 PowerShell 中工作时,可以访问对象的所有成员。

若要仅获取对象的属性,而不获取方法的属性,请使用值为“property”的 cmdlet 的 Get-Member MemberType 参数,如以下示例所示。

Get-ChildItem $pshome\PowerShell.exe | Get-Member -MemberType property
TypeName: System.IO.FileInfo

Name              MemberType Definition
----              ---------- ----------
Attributes        Property   System.IO.FileAttributes Attributes {get;set;}
CreationTime      Property   System.DateTime CreationTime {get;set;}
CreationTimeUtc   Property   System.DateTime CreationTimeUtc {get;set;}
Directory         Property   System.IO.DirectoryInfo Directory {get;}
DirectoryName     Property   System.String DirectoryName {get;}
Exists            Property   System.Boolean Exists {get;}
Extension         Property   System.String Extension {get;}
FullName          Property   System.String FullName {get;}
IsReadOnly        Property   System.Boolean IsReadOnly {get;set;}
LastAccessTime    Property   System.DateTime LastAccessTime {get;set;}
LastAccessTimeUtc Property   System.DateTime LastAccessTimeUtc {get;set;}
LastWriteTime     Property   System.DateTime LastWriteTime {get;set;}
LastWriteTimeUtc  Property   System.DateTime LastWriteTimeUtc {get;set;}
Length            Property   System.Int64 Length {get;}
Name              Property   System.String Name {get;}

找到属性后,可以在 PowerShell 命令中使用它们。

属性值

尽管特定类型的每个对象都具有相同的属性,但这些属性的值会描述特定对象。 例如,每个 FileInfo 对象都有一个 CreationTime 属性,但该属性的值因每个文件而异。

获取对象的属性值的最常见方法是使用点方法。 键入对 对象的引用,例如包含 对象的变量或获取 对象的命令。 然后,键入一个点 (.) ,后跟属性名称。

例如,以下命令显示 PowerShell.exe 文件的 CreationTime 属性的值。 该 Get-ChildItem 命令返回一个 FileInfo 对象,该对象代表 PowerShell.exe 文件。 命令括在括号中,以确保在访问任何属性之前执行该命令。 命令 Get-ChildItem 后跟一个点和 CreationTime 属性的名称,如下所示:

(Get-ChildItem $pshome\PowerShell.exe).creationtime
Tuesday, March 18, 2008 12:07:52 AM

还可以将对象保存在变量中,然后使用 dot 方法获取其属性,如以下示例所示:

$a = Get-ChildItem $pshome\PowerShell.exe
$a.CreationTime
Tuesday, March 18, 2008 12:07:52 AM

还可以使用 Select-ObjectFormat-List cmdlet 显示对象的属性值。 Select-ObjectFormat-List 每个 都有 一个 Property 参数。 可以使用 Property 参数指定一个或多个属性及其值。 或者,可以使用通配符 (*) 来表示所有属性。

例如,以下命令显示 PowerShell.exe 文件的所有属性的值。

Get-ChildItem $pshome\PowerShell.exe | Format-List -Property *
PSPath            : Microsoft.PowerShell.Core\FileSystem::C:\Windows\System3
                    2\WindowsPowerShell\v1.0\PowerShell.exe
PSParentPath      : Microsoft.PowerShell.Core\FileSystem::C:\Windows\System3
                    2\WindowsPowerShell\v1.0
PSChildName       : PowerShell.exe
PSDrive           : C
PSProvider        : Microsoft.PowerShell.Core\FileSystem
PSIsContainer     : False
Mode              : -a----
VersionInfo       : File:             C:\Windows\System32\WindowsPowerShell\
                    v1.0\PowerShell.exe
                    InternalName:     POWERSHELL
                    OriginalFilename: PowerShell.EXE.MUI
                    FileVersion:      10.0.16299.15 (WinBuild.160101.0800)
                    FileDescription:  Windows PowerShell
                    Product:          Microsoft Windows Operating System
                    ProductVersion:   10.0.16299.15
                    Debug:            False
                    Patched:          False
                    PreRelease:       False
                    PrivateBuild:     False
                    SpecialBuild:     False
                    Language:         English (United States)

BaseName          : PowerShell
Target            : {C:\Windows\WinSxS\amd64_microsoft-windows-powershell-ex
                    e_31bf3856ad364e35_10.0.16299.15_none_8c022aa6735716ae\p
                    owershell.exe}
LinkType          : HardLink
Name              : PowerShell.exe
Length            : 449024
DirectoryName     : C:\Windows\System32\WindowsPowerShell\v1.0
Directory         : C:\Windows\System32\WindowsPowerShell\v1.0
IsReadOnly        : False
Exists            : True
FullName          : C:\Windows\System32\WindowsPowerShell\v1.0\PowerShell.ex
Extension         : .exe
CreationTime      : 9/29/2017 6:43:19 AM
CreationTimeUtc   : 9/29/2017 1:43:19 PM
LastAccessTime    : 9/29/2017 6:43:19 AM
LastAccessTimeUtc : 9/29/2017 1:43:19 PM
LastWriteTime     : 9/29/2017 6:43:19 AM
LastWriteTimeUtc  : 9/29/2017 1:43:19 PM
Attributes        : Archive

静态属性

可以在 PowerShell 中使用 .NET 类的静态属性。 静态属性是类的属性,与标准属性不同,标准属性是对象的属性。

若要获取类的静态属性,请使用 Get-Member cmdlet 的 Static 参数。

例如,以下命令获取 类的 System.DateTime 静态属性。

Get-Date | Get-Member -MemberType Property -Static
TypeName: System.DateTime

Name     MemberType Definition
----     ---------- ----------
MaxValue Property   static datetime MaxValue {get;}
MinValue Property   static datetime MinValue {get;}
Now      Property   datetime Now {get;}
Today    Property   datetime Today {get;}
UtcNow   Property   datetime UtcNow {get;}

若要获取静态属性的值,请使用以下语法。

[<ClassName>]::<Property>

例如,以下命令获取 类的 UtcNow 静态属性 System.DateTime 的值。

[System.DateTime]::UtcNow

标量对象和集合的属性

特定类型的一个 (“scalar”) 对象的属性通常不同于同一类型的对象集合的属性。 例如,每个服务都具有 作为 DisplayName 属性,但服务集合没有 DisplayName 属性。

以下命令获取“Audiosrv”服务的 DisplayName 属性的值。

(Get-Service Audiosrv).DisplayName
Windows Audio

从 PowerShell 3.0 开始,PowerShell 会尝试防止标量对象和集合的不同属性导致的脚本错误。 同一命令返回返回的每个服务的 Get-ServiceDisplayName 属性的值。

(Get-Service).DisplayName
Application Experience
Application Layer Gateway Service
Windows All-User Install Agent
Application Identity
Application Information
...

如果提交集合,但请求仅在单个 (“scalar”) 对象上存在的属性,PowerShell 会为集合中的每个对象返回该属性的值。

所有集合都有 一个 Count 属性,该属性返回集合中的对象数。

(Get-Service).Count
176

从 PowerShell 3.0 开始,如果请求零个对象或一个对象的 Count 或 Length 属性,PowerShell 将返回正确的值。

(Get-Service Audiosrv).Count
1

如果单个对象和集合上存在属性,则仅返回集合的 属性。

$collection = @(
[pscustomobject]@{length = "foo"}
[pscustomobject]@{length = "bar"}
)
# PowerShell returns the collection's Length.
$collection.length
2

此功能也适用于标量对象和集合的方法。 有关详细信息,请参阅 about_Methods

另请参阅

about_Methods

about_Objects

Get-Member

Select-Object

Format-List