about_Properties

適用於: Windows PowerShell 2.0, Windows PowerShell 3.0, Windows PowerShell 4.0, Windows PowerShell 5.0

主題

about_Properties

簡短描述

描述如何使用 Windows PowerShell 中的物件屬性。

詳細描述

Windows PowerShell 使用稱為物件的資訊結構化集合,來代表資料存放區或是電腦狀態中的項目。通常您可使用屬於 Microsoft.NET Framework 的物件,但也可以在 Windows PowerShell 建立自訂物件。

項目和其物件之間的關聯非常密切。當您變更物件時,通常會變更它所代表的項目。例如,當您取得 Windows PowerShell 中的檔案時,您不會取得實際的檔案。相反地,您會取得 FileInfo 物件,表示該檔案。當您變更 FileInfo 物件時,該檔案會隨之變更。

大部分的物件具有屬性。屬性是與物件相關聯的資料。不同類型的物件有不同的屬性。例如代表檔案的 FileInfo 物件具有 IsReadOnly 屬性,如果該檔案包含唯讀屬性,則包含 $True,反之則為 $False。代表檔案系統目錄的 DirectoryInfo 物件有包含上層目錄路徑的父屬性。

物件屬性

若要取得物件的屬性,請使用 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 命令會傳回 FileInfo 物件,其代表 PowerShell.exe 檔案。該命令會以括弧括住,以確定它會在存取任何屬性之前執行。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
        

靜態屬性

您可以使用 Windows 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

純量物件和集合的屬性

特定類型的一個 (「純量」) 物件屬性通常和相同類型物件集合的屬性不同。

例如,每項服務都有 DisplayName 屬性,但服務的集合並沒有 DisplayName 屬性。同樣地,所有集合都具有 Count 屬性,可告知該集合中有多少物件,但個別物件並沒有 Count 屬性。

從 Windows PowerShell 3.0 開始,Windows PowerShell 會嘗試防止純量物件和集合屬性不同所導致的指令碼錯誤。

-- 如果您送出一個集合,但是要求只存在於單一 (「純量」) 物件的屬性,則 Windows PowerShell 會傳回此集合中每個物件的該屬性值。

-- 如果您要求無物件或一個物件的 Count 或 Length 屬性,則 Windows PowerShell 會傳回正確的值。

如果此屬性存在於個別物件和該集合上,則 Windows PowerShell 不會改變此結果。

這項功能也適用於純量物件和集合的方法。如需詳細資訊,請參閱 about_Methods。

範例

例如,每項服務都有 DisplayName 屬性。下列命令會取得 Audiosrv 服務的 DisplayName 屬性值。

        PS C:\>(Get-Service Audiosrv).DisplayName
        Windows Audio

不過,服務的集合或陣列並沒有 DisplayName。下列命令會嘗試取得 Windows PowerShell 2.0 中所有服務的 DisplayName 屬性。

        PS C:\>(Get-Service).DisplayName
        PS C:\>

從 Windows PowerShell 3.0 開始,相同命令所傳回的 DisplayName 屬性值為 Get-Service 傳回每項服務的屬性值。

        PS C:\>(Get-Service).DisplayName
        Application Experience
        Application Layer Gateway Service
        Windows All-User Install Agent
        Application Identity
        Application Information
        ...

相反地,兩項或多項服務的集合有 Count 屬性,包含集合中的物件數目。

        PS C:\>(Get-Service).Count
        176

個別服務沒有 Count 或 Length 屬性,如 Windows PowerShell 2.0 中的這個命令所示。

        PS C:\>(Get-Service Audiosrv).Count
        PS C:\>

從 Windows PowerShell 3.0 開始,此命令會傳回正確的 Count 值。

        PS C:\>(Get-Service Audiosrv).Count
        1

另請參閱

about_Methods

about_Objects

Get-Member

Select-Object

Format-List