about_Properties

简短说明

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

长说明

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

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

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

对象属性

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

Get-ChildItem $PSHOME\pwsh.exe | Get-Member

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

如果只要获取对象的属性而不获取方法,请使用值为 PropertyGet-Member cmdlet 的 MemberType 参数,如以下示例所示。

Get-ChildItem $PSHOME\pwsh.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 属性,但每个文件的该属性的值不同。

获取对象属性值的最常见方法是使用成员访问运算符 (.)。 键入对该对象的引用,例如一个包含该对象的变量或者一个获取该对象的命令。 然后,键入运算符 (.),后接属性名称。

例如,以下命令显示 pwsh.exe 文件的 CreationTime 属性的值。 Get-ChildItem 命令返回表示 pwsh.exe file 的 FileInfo 对象。 该命令括在括号中,这样可以确保在访问任何属性之前执行该命令。

(Get-ChildItem $PSHOME\pwsh.exe).CreationTime
Wednesday, June 15, 2022 5:26:00 PM

还可以将对象保存在变量中,然后使用成员访问 (.) 方法获取其属性,如以下示例所示:

$a = Get-ChildItem $PSHOME\pwsh.exe
$a.CreationTime
Wednesday, June 15, 2022 5:26:00 PM

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

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

Get-ChildItem $PSHOME\pwsh.exe | Format-List -Property *
PSPath              : Microsoft.PowerShell.Core\FileSystem::C:\Program Files\PowerShell\7\pwsh.exe
PSParentPath        : Microsoft.PowerShell.Core\FileSystem::C:\Program Files\PowerShell\7
PSChildName         : pwsh.exe
PSDrive             : C
PSProvider          : Microsoft.PowerShell.Core\FileSystem
PSIsContainer       : False
Mode                : -a---
ModeWithoutHardLink : -a---
VersionInfo         : File:             C:\Program Files\PowerShell\7\pwsh.exe
                      InternalName:     pwsh.dll
                      OriginalFilename: pwsh.dll
                      FileVersion:      7.2.5.500
                      FileDescription:  pwsh
                      Product:          PowerShell
                      ProductVersion:   7.2.5 SHA: 0aad398b0e918ce7d73dca929ca6395639085b21
                      Debug:            False
                      Patched:          False
                      PreRelease:       False
                      PrivateBuild:     False
                      SpecialBuild:     False
                      Language:         Language Neutral

BaseName            : pwsh
Target              :
LinkType            :
Length              : 287648
DirectoryName       : C:\Program Files\PowerShell\7
Directory           : C:\Program Files\PowerShell\7
IsReadOnly          : False
FullName            : C:\Program Files\PowerShell\7\pwsh.exe
Extension           : .exe
Name                : pwsh.exe
Exists              : True
CreationTime        : 6/15/2022 5:26:00 PM
CreationTimeUtc     : 6/15/2022 10:26:00 PM
LastAccessTime      : 7/18/2022 11:32:06 AM
LastAccessTimeUtc   : 7/18/2022 4:32:06 PM
LastWriteTime       : 6/15/2022 5:26:00 PM
LastWriteTimeUtc    : 6/15/2022 10:26:00 PM
LinkTarget          :
Attributes          : Archive

静态属性

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

若要获取类的静态属性,请使用 cmdlet 的 Get-Member 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>

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

[System.DateTime]::UtcNow

成员访问枚举

从 PowerShell 3.0 开始,当你使用成员访问运算符 (.) 访问列表集合中不存在的属性时,PowerShell 会自动枚举集合中的项并返回每个项的属性值。 有关详细信息,请参阅 about_Member-Access_Enumeration

示例

此命令返回 Get-Service 所返回的每个服务的 DisplayName 属性的值。

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

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

(Get-Service).Count
176

从 PowerShell 3.0 开始,可以获取非集合的单一实例对象的 Count 或 Length 属性。

(Get-Service Audiosrv).Count
1

不过,某些对象具有 Length 属性。 例如,字符串的 Length 是字符串中的字符数。 Count 属性是对象的实例数。

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

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

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

另请参阅