共用方式為


about_Properties

簡短描述

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

完整描述

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

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

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

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

$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\pwsh.exe
PSParentPath        : Microsoft.PowerShell.Core\FileSystem::C:\Program Files\PowerShell\7
PSChildName         : pwsh.exe
PSDrive             : C
PSProvider          : Microsoft.PowerShell.Core\FileSystem
PSIsContainer       : False
Mode                : -a---
ModeWithoutHardLink : -a---
VersionInfo         : File:             C:\Program Files\PowerShell\7\pwsh.exe
                      InternalName:     pwsh.dll
                      OriginalFilename: pwsh.dll
                      FileVersion:      7.3.9.500
                      FileDescription:  pwsh
                      Product:          PowerShell
                      ProductVersion:   7.3.9 SHA: 116d193ed28dcc6914653c799846bbf379cea0fb
                      Debug:            False
                      Patched:          False
                      PreRelease:       False
                      PrivateBuild:     False
                      SpecialBuild:     False
                      Language:         Language Neutral

BaseName            : pwsh
ResolvedTarget      : C:\Program Files\PowerShell\7\pwsh.exe
Target              :
LinkType            :
Length              : 293312
DirectoryName       : C:\Program Files\PowerShell\7
Directory           : C:\Program Files\PowerShell\7
IsReadOnly          : False
FullName            : C:\Program Files\PowerShell\7\pwsh.exe
Extension           : .exe
Name                : pwsh.exe
Exists              : True
CreationTime        : 10/25/2023 7:00:18 PM
CreationTimeUtc     : 10/26/2023 12:00:18 AM
LastAccessTime      : 11/14/2023 5:14:36 PM
LastAccessTimeUtc   : 11/14/2023 11:14:36 PM
LastWriteTime       : 10/25/2023 7:00:18 PM
LastWriteTimeUtc    : 10/26/2023 12:00:18 AM
LinkTarget          :
UnixFileMode        : -1
Attributes          : Archive

靜態屬性

您可以在 PowerShell 中使用 .NET 類別的靜態屬性。 靜態屬性是類別的屬性,有別於標準屬性,它們是物件的屬性。

若要取得類別的靜態屬性,請使用 Cmdlet 的 Get-MemberStatic 參數。 例如,下列命令會取得 類別的 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.DateTimeUtcNow靜態屬性值。

[System.DateTime]::UtcNow

成員存取列舉

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

範例

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

(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

另請參閱