about_Properties

简短说明

介绍如何使用 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。 以下命令获取 pwsh.exe 文件并将其发送到 Get-Member$PSHOME 自动变量包含 PowerShell 安装目录的路径。

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

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

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

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 属性的值。 Get-ChildItem 命令返回表示 pwsh.exe file 对象。 该命令括在括号中,以确保在访问任何属性之前执行该命令。

(Get-ChildItem $PSHOME\pwsh.exe).CreationTime
Tuesday, June 14, 2022 5:17:14 PM

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

$a = Get-ChildItem $PSHOME\pwsh.exe
$a.CreationTime
Tuesday, June 14, 2022 5:17:14 PM

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

例如,以下命令显示 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.3.9.500
                      FileDescription:  pwsh
                      Product:          PowerShell
                      ProductVersion:   7.3.9 SHA: 116d193ed28dcc6914653c799846bbf379cea0fb
                      Debug:            False
                      Patched:          False
                      PreRelease:       False
                      PrivateBuild:     False
                      SpecialBuild:     False
                      Language:         Language Neutral

BaseName            : pwsh
ResolvedTarget      : C:\Program Files\PowerShell\7\pwsh.exe
Target              :
LinkType            :
Length              : 293312
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        : 10/25/2023 7:00:18 PM
CreationTimeUtc     : 10/26/2023 12:00:18 AM
LastAccessTime      : 11/14/2023 5:14:36 PM
LastAccessTimeUtc   : 11/14/2023 11:14:36 PM
LastWriteTime       : 10/25/2023 7:00:18 PM
LastWriteTimeUtc    : 10/26/2023 12:00:18 AM
LinkTarget          :
UnixFileMode        : -1
Attributes          : Archive

静态属性

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

若要获取类的静态属性,请使用 cmdlet 的 Get-Member 参数。 例如,以下命令获取 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 静态属性的值。

[System.DateTime]::UtcNow

成员访问枚举

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

例子

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

(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 开始,可以获取非集合的单一实例对象的 CountLength 属性。

(Get-Service Audiosrv).Count
1

但是,某些对象具有 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

另请参阅