简短说明
介绍如何使用 PowerShell 中的对象属性。
详细说明
PowerShell 使用称为对象的结构化信息集合来表示数据存储或计算机状态中的项。 通常,您使用的是 Microsoft .NET Framework 中的对象,但您也可以在 PowerShell 中创建自定义对象。
项与其对象之间的关联非常接近。 更改对象时,通常会更改它所表示的项。 例如,当您在 PowerShell 中获取文件时,您不会获得实际的文件。 相反,您将获得一个表示文件的 FileInfo 对象。 更改 FileInfo 对象时,文件也会更改。
大多数对象都具有属性。 Properties 是与对象关联的数据。 不同类型的对象具有不同的属性。 例如,表示文件的 FileInfo 对象具有一个 IsReadOnly 属性,如果文件是只读属性,则包含 $True 属性,如果文件不是只读属性,则包含 $False。 表示文件系统目录的 DirectoryInfo 对象具有包含父目录路径的 Parent 属性。
对象属性
若要获取对象的属性,请使用 Get-Member cmdlet。 例如,若要获取 FileInfo 对象的属性,请使用 Get-ChildItem cmdlet 获取表示文件的 FileInfo 对象。 然后,使用管道运算符 (|) 将 FileInfo 对象 Get-Member发送到 。 以下命令获取 PowerShell.exe 文件并将其发送到 Get-Member。
$Pshome automatic 变量包含 PowerShell 安装目录的路径。
Get-ChildItem $pshome\PowerShell.exe | Get-Member
命令的输出列出了 FileInfo 对象的成员。 成员包括属性和方法。 在 PowerShell 中工作时,可以访问对象的所有成员。
若要仅获取对象的属性而不获取方法,请使用 cmdlet 的 Get-Member MemberType 参数,其值为 “property”,如以下示例所示。
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 属性,但该属性的值对于每个文件都不同。
获取对象属性值的最常见方法是使用 dot 方法。 键入对对象的引用,例如包含对象的变量或获取对象的命令。 然后,键入一个点 (.),后跟属性名称。
例如,以下命令显示 PowerShell.exe 文件的 CreationTime 属性的值。 该 Get-ChildItem 命令返回表示 PowerShell.exe 文件的 FileInfo 对象。 该命令括在括号中,以确保在访问任何属性之前执行该命令。 该 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-Object 和 Format-List cmdlet 来显示对象的属性值。 每个 Select-Object 和 Format-List 都有一个 属性 参数。 可以使用 属性 参数指定一个或多个属性及其值。 或者,您可以使用通配符 (*) 来表示所有属性。
例如,以下命令显示 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>
例如,以下命令获取类的 System.DateTime UtcNow 静态属性的值。
[System.DateTime]::UtcNow
标量对象和集合的属性
特定类型的一个(“标量”)对象的属性通常与相同类型的对象集合的属性不同。 例如,每个服务都具有 as DisplayName 属性,但服务集合没有 DisplayName 属性。
以下命令获取“Audiosrv”服务的 DisplayName 属性的值。
(Get-Service Audiosrv).DisplayName
Windows Audio
从 PowerShell 3.0 开始,PowerShell 会尝试防止因标量对象和集合的不同属性而导致的脚本错误。 同一命令返回返回的每个服务的 Get-Service 属性的值。
(Get-Service).DisplayName
Application Experience
Application Layer Gateway Service
Windows All-User Install Agent
Application Identity
Application Information
...
提交集合,但请求仅存在于单个(“标量”)对象上的属性时,PowerShell 会返回集合中每个对象的该属性的值。
所有集合都有一个 Count 属性,该属性返回集合中的对象数。
(Get-Service).Count
176
从 PowerShell 3.0 开始,如果请求零个对象或 1 个对象的 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。