主题
    about_Properties

简短说明
    描述如何在 Windows PowerShell 中使用对象属性。


详细说明
    Windows PowerShell 使用结构化的信息集合(称为对象)来表示数据存储中的项或计算机的状态。
    通常情况下,您使用属于 Microsoft .NET Framework 的对象,但是您也可以在 Windows PowerShell 中
    创建自定义对象。


    项与其对象之间的关联非常紧密。当您更改对象时,您是在更改对象所表示的项。例如,当您在 Windows 
    PowerShell 中获取文件时,您得到的并不是实际文件。
    您获得的是表示该文件的 FileInfo 对象。当您更改 FileInfo 对象时,文件将随之更改。


    大多数对象都有属性。属性是与对象关联的数据。这些数据用于描述对象。例如,FileInfo 对象有名为 Length 
    的属性,该属性描述此对象所表示的文件的大小。


  对象属性

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

         get-childitem $pshome\powershell.exe |get-member


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


     若只获取对象的属性而不获取其方法,请使用 Get-Member cmdlet 的 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;}

     发现属性之后,可以在 Windows PowerShell 命令中使用它们。


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


     获得对象属性值的最常见途径是使用点方法。键入对象的引用(如包含该对象的变量),或者键入可获取该对象的
     命令。然后,键入一个点 (.),后面跟随属性名。


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

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


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

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


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


     例如,下面的命令显示了 PowerShell.exe 文件所有属性的值。
 
 
         C:\PS> Get-ChildItem $pshome\powershell.exe | Format-List -property *

         PSPath            : Microsoft.PowerShell.Core\FileSystem::C:\Windows\system32\WindowsPowerShell\v1.0\powershell.exe
         PSParentPath      : Microsoft.PowerShell.Core\FileSystem::C:\Windows\system32\WindowsPowerShell\v1.0
         PSChildName       : powershell.exe
         PSDrive           : C
         PSProvider        : Microsoft.PowerShell.Core\FileSystem
         PSIsContainer     : False
         VersionInfo       : File:             C:\Windows\system32\WindowsPowerShell\v1.0\powershell.exe
                             InternalName:     POWERSHELL
                             OriginalFilename: PowerShell.EXE.MUI
                             File Version:      6.1.6570.1 (fbl_srv_powershell(nigels).070711-0102)
                             FileDescription:  PowerShell.EXE
                             Product:          Microsoft® Windows® Operating System
                             ProductVersion:   6.1.6570.1
                             Debug:            False
                             Patched:          False
                             PreRelease:       False
                             PrivateBuild:     True
                             SpecialBuild:     False
                             Language:         English (United States)

         BaseName          : powershell
         Mode              : -a---
         Name              : powershell.exe
         Length            : 160256
         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.exe
         Extension         : .exe
         CreationTime      : 3/18/2008 12:07:52 AM
         CreationTimeUtc   : 3/18/2008 7:07:52 AM
         LastAccessTime    : 3/19/2008 8:13:58 AM
         LastAccessTimeUtc : 3/19/2008 3:13:58 PM
         LastWriteTime     : 3/18/2008 12:07:52 AM
         LastWriteTimeUtc  : 3/18/2008 7:07:52 AM
         Attributes        : Archive
        

另请参阅
    about_Objects
    Get-Member
    Select-Object
    Format-List




目录