主題
    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:         中文 (台灣)

         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      : 2008/3/18 上午 12:07:52
         CreationTimeUtc   : 2008/3/18 上午 7:07:52
         LastAccessTime    : 2008/3/19 上午 8:13:58
         LastAccessTimeUtc : 2008/3/19 下午 3:13:58
         LastWriteTime     : 2008/3/18 上午 12:07:52
         LastWriteTimeUtc  : 2008/3/18 上午 7:07:52
         Attributes        : Archive
        

請參閱
    about_Objects
    Get-Member
    Select-Object
    Format-List




目錄