簡短描述
描述如何在PowerShell中使用物件屬性。
完整描述
PowerShell 會使用稱為 對象的結構化資訊集合來表示數據存放區或計算機狀態中的專案。 通常,您使用的是 Microsoft .NET Framework 中的物件,但您也可以在 PowerShell 中創建自定義物件。
專案與其對象之間的關聯非常接近。 當您變更物件時,通常會變更它所代表的專案。 例如,當您在 PowerShell 中獲取檔時,您不會獲得實際的檔。 相反,您將獲得一個表示檔的 FileInfo 物件。 當您變更 FileInfo 物件時,檔案也會變更。
大部分的物件都有屬性。 Properties 是與對象關聯的數據。 不同類型的物件有不同的屬性。 例如,表示檔的 FileInfo 物件具有一個 IsReadOnly 屬性,如果檔是只讀屬性,則包含 $True 屬性,如果檔不是唯讀屬性,則包含 $False。 表示文件系統目錄的 DirectoryInfo 物件具有包含父目錄路徑的 Parent 屬性。
物件內容
若要取得對象的屬性,請使用 Get-Member Cmdlet。 例如,若要取得 fileInfo 物件的 屬性,請使用 Get-ChildItem Cmdlet 來取得代表檔案的 FileInfo 物件。 然後,使用管道運算子 (|) 將 FileInfo 物件 Get-Member送出到 。 以下命令取得 PowerShell.exe 檔案並將送出到 Get-Member。
$Pshome automatic 變數包含 PowerShell 安裝目錄的路徑。
Get-ChildItem $pshome\PowerShell.exe | Get-Member
命令的輸出會列出 fileInfo 物件 的成員。 成員同時包含屬性和方法。 當您在 PowerShell 中工作時,您可以存取物件的所有成員。
若要僅獲取對象的屬性而不獲取方法,請使用 cmdlet 的 Get-Member 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;}
找到屬性之後,您可以在 PowerShell 命令中使用它們。
屬性值
雖然特定類型的每個物件都有相同的屬性,但這些屬性的值會描述特定物件。 例如,每個 FileInfo 物件都有一個 CreationTime 屬性,但該屬性的值對於每個檔都不同。
獲取物件屬性值的最常見方法是使用 dot 方法。 輸入 對象的參考,例如包含 物件的變數,或取得 物件的命令。 然後,鍵入一個點 (.),後跟屬性名稱。
例如,以下命令顯示 PowerShell.exe 檔的 CreationTime 屬性的值。 該 Get-ChildItem 命令返回表示 PowerShell.exe 檔的 FileInfo 物件。 該命令括在括弧中,以確保在訪問任何屬性之前執行該命令。 該 Get-ChildItem 命令後跟一個點和 CreationTime 屬性的名稱,如下所示:
(Get-ChildItem $pshome\PowerShell.exe).creationtime
Tuesday, March 18, 2008 12:07:52 AM
您還可以將物件儲存在變數中,然後使用 dot 方法獲取其屬性,如以下範例所示:
$a = Get-ChildItem $pshome\PowerShell.exe
$a.CreationTime
Tuesday, March 18, 2008 12:07:52 AM
您也可以使用 Select-Object 和 Format-List Cmdlet 來顯示物件的屬性值。
Select-Object 和 Format-List 各有一個 Property 參數。 您可以使用 Property 參數來指定一或多個屬性及其值。 或者,您可以使用通配符 (*) 來代表所有屬性。
例如,以下命令顯示 PowerShell.exe 檔的所有屬性的值。
Get-ChildItem $pshome\PowerShell.exe | Format-List -Property *
PSPath : Microsoft.PowerShell.Core\FileSystem::C:\Windows\System3
2\WindowsPowerShell\v1.0\PowerShell.exe
PSParentPath : Microsoft.PowerShell.Core\FileSystem::C:\Windows\System3
2\WindowsPowerShell\v1.0
PSChildName : PowerShell.exe
PSDrive : C
PSProvider : Microsoft.PowerShell.Core\FileSystem
PSIsContainer : False
Mode : -a----
VersionInfo : File: C:\Windows\System32\WindowsPowerShell\
v1.0\PowerShell.exe
InternalName: POWERSHELL
OriginalFilename: PowerShell.EXE.MUI
FileVersion: 10.0.16299.15 (WinBuild.160101.0800)
FileDescription: Windows PowerShell
Product: Microsoft Windows Operating System
ProductVersion: 10.0.16299.15
Debug: False
Patched: False
PreRelease: False
PrivateBuild: False
SpecialBuild: False
Language: English (United States)
BaseName : PowerShell
Target : {C:\Windows\WinSxS\amd64_microsoft-windows-powershell-ex
e_31bf3856ad364e35_10.0.16299.15_none_8c022aa6735716ae\p
owershell.exe}
LinkType : HardLink
Name : PowerShell.exe
Length : 449024
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.ex
Extension : .exe
CreationTime : 9/29/2017 6:43:19 AM
CreationTimeUtc : 9/29/2017 1:43:19 PM
LastAccessTime : 9/29/2017 6:43:19 AM
LastAccessTimeUtc : 9/29/2017 1:43:19 PM
LastWriteTime : 9/29/2017 6:43:19 AM
LastWriteTimeUtc : 9/29/2017 1:43:19 PM
Attributes : Archive
靜態屬性
您可以在 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
成員訪問枚舉
從 PowerShell 3.0 開始,當您使用成員訪問運算子 (). 訪問清單集合上不存在的屬性時,PowerShell 會自動枚舉集合中的項,並返回每個項上的屬性值。 如需詳細資訊,請參閱 about_Member-Access_Enumeration。
範例
此命令會傳回每個 傳回之服務的 Get-Service 屬性值。
(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 開始,如果請求零個物件或 1 個物件的 Count 或 Length 屬性,PowerShell 將返回正確的值。
(Get-Service Audiosrv).Count
1
如果個別物件和集合上有屬性,則只會傳回集合的屬性。
$collection = @(
[pscustomobject]@{length = "foo"}
[pscustomobject]@{length = "bar"}
)
# PowerShell returns the collection's Length.
$collection.length
2