Share via


about_Properties

簡短描述

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

詳細描述

PowerShell 會使用稱為 對象的結構化資訊集合來表示數據存放區或計算機狀態中的專案。 一般而言,您可以使用屬於 Microsoft .NET Framework 一部分的物件,但您也可以在 PowerShell 中建立自定義物件。

專案與其對象之間的關聯非常接近。 當您變更物件時,通常會變更它所代表的專案。 例如,當您在PowerShell中取得檔案時,不會取得實際的檔案。 相反地,您會取得 代表檔案的 FileInfo 物件。 當您變更 FileInfo 物件時,檔案也會變更。

大部分的物件都有屬性。 屬性是與 對象相關聯的數據。 不同類型的物件有不同的屬性。 例如,代表檔案的 FileInfo 物件具有 IsReadOnly 屬性,如果檔案具有唯讀屬性,如果$False檔案沒有,則為 $True代表文件系統目錄的 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 MemberType 參數,其值為 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 屬性,但該屬性的值會因每個檔案而有所不同。

取得物件屬性值的最常見方式是使用成員存取運算符 (.)。 輸入 對象的參考,例如包含 物件的變數,或取得 物件的命令。 然後,輸入運算子 (.) 後面接著屬性名稱。

例如,下列命令會顯示 檔案的 CreationTime 屬性值 pwsh.exe 。 此命令Get-ChildItem會傳回代表的 pwsh.exe fileFileInfo 物件。 命令會以括弧括住,以確保它會在存取任何屬性之前執行。

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

您也可以將物件儲存在變數中,然後使用成員 access (.) 方法來取得其屬性,如下列範例所示:

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

您也可以使用 Select-ObjectFormat-List Cmdlet 來顯示 物件的屬性值。 Select-ObjectFormat-List 各有 Property 參數。 您可以使用 Property 參數來指定一或多個屬性及其值。 或者,您可以使用通配符 (*) 來代表所有屬性。

例如,下列命令會顯示pwsh.exe檔案之所有屬性的值。

Get-ChildItem $PSHOME\pwsh.exe | Format-List -Property *
PSPath              : Microsoft.PowerShell.Core\FileSystem::C:\Program Files\PowerShell\7-preview\pwsh.exe
PSParentPath        : Microsoft.PowerShell.Core\FileSystem::C:\Program Files\PowerShell\7-preview
PSChildName         : pwsh.exe
PSDrive             : C
PSProvider          : Microsoft.PowerShell.Core\FileSystem
PSIsContainer       : False
Mode                : -a---
ModeWithoutHardLink : -a---
VersionInfo         : File:             C:\Program Files\PowerShell\7-preview\pwsh.exe
                      InternalName:     pwsh.dll
                      OriginalFilename: pwsh.dll
                      FileVersion:      7.3.0.5
                      FileDescription:  pwsh
                      Product:          PowerShell
                      ProductVersion:   7.3.0-preview.5 SHA: cfc237ac85cf24fa760923ace568201c8f3256aa
                      Debug:            False
                      Patched:          False
                      PreRelease:       False
                      PrivateBuild:     False
                      SpecialBuild:     False
                      Language:         Language Neutral

BaseName            : pwsh
ResolvedTarget      : C:\Program Files\PowerShell\7-preview\pwsh.exe
Target              :
LinkType            :
Length              : 285088
DirectoryName       : C:\Program Files\PowerShell\7-preview
Directory           : C:\Program Files\PowerShell\7-preview
IsReadOnly          : False
FullName            : C:\Program Files\PowerShell\7-preview\pwsh.exe
Extension           : .exe
Name                : pwsh.exe
Exists              : True
CreationTime        : 6/14/2022 5:17:14 PM
CreationTimeUtc     : 6/14/2022 10:17:14 PM
LastAccessTime      : 7/18/2022 11:32:06 AM
LastAccessTimeUtc   : 7/18/2022 4:32:06 PM
LastWriteTime       : 6/14/2022 5:17:14 PM
LastWriteTimeUtc    : 6/14/2022 10:17:14 PM
LinkTarget          :
Attributes          : Archive

靜態屬性

您可以在 PowerShell 中使用 .NET 類別的靜態屬性。 靜態屬性是 類別的屬性,不同於標準屬性,這些屬性是 對象的屬性。

若要取得類別的靜態屬性,請使用 Cmdlet 的 Get-Member 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

成員存取列舉

從 PowerShell 3.0 開始,當您使用成員存取運算子 (.) 來存取清單集合上不存在的屬性時,PowerShell 會自動列舉集合中的專案,並傳回每個專案上的 屬性值。 如需詳細資訊,請參閱 about_Member-Access_Enumeration

範例

此命令會傳回每個傳回之服務的 Get-Service DisplayName 屬性值。

(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 屬性。 例如, 字串的 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

另請參閱