about_Type_Operators
簡短描述
描述使用Microsoft .NET 類型的運算符。
詳細描述
布爾型別運算子 (-is
和 -isnot
) 會指出物件是否為指定 .NET 類型的實例。
-is
如果型別符合,則運算符會傳回 TRUE 的值,否則傳回 FALSE 的值。
-isnot
如果型別符合,則運算符會傳回 FALSE 的值,否則傳回 TRUE 的值。
運算子 -as
會嘗試將輸入物件轉換成指定的 .NET 類型。 如果成功,則會傳回已轉換的物件。 如果失敗,則會傳回 $null
。 它不會傳回錯誤。
PowerShell 具有下列類型運算子:
-is
|當輸入是指定之 .NET 類型的實例時,傳回TRUE。(get-date) -is [DateTime] # Result is True
-isnot
|當輸入不是 specified.NET 類型的實例時,傳回 TRUE。(get-date) -isnot [DateTime] # Result is False
-as
|將輸入轉換為指定的 .NET 類型。"5/7/07" -as [DateTime] # Result is Monday, May 7, 2007 12:00:00 AM
類型運算子的語法如下所示:
<input> <operator> [.NET type]
您也可以使用下列語法:
<input> <operator> ".NET type"
.NET 類型可以寫入為方括弧或字串中的類型名稱,例如 [DateTime]
System.DateTime"DateTime"
或 。 如果類型不在系統命名空間的根目錄,請指定物件類型的完整名稱。 您可以省略 「System.」。。 例如,若要指定 System.Diagnostics.Process,請輸入 [System.Diagnostics.Process]
、 [Diagnostics.Process]
或 "Diagnostics.Process"
。
類型運算元一律會以整體方式在輸入物件上操作。 也就是說,如果輸入對像是集合,則它是測試的集合類型,而不是集合專案的型別。
-is/isot 運算符
布爾型別運算符 (-is
和 -isnot
) 一律會傳回布爾值,即使輸入是 物件的集合也一樣。
如果 <input>
是與或 衍生自 .NET 類型的類型相同,運算符會-is
傳$True
回 。
例如, DirectoryInfo 類型衍生自 FileSystemInfo 類型。 因此,這兩個範例都會傳回 True。
PS> (Get-Item /) -is [System.IO.DirectoryInfo]
True
PS> (Get-Item /) -is [System.IO.FileSystemInfo]
True
如果 -is
實作比較中的 介面,運算子<input>
也可以比對接口。 在此範例中,輸入是陣列。 陣列會實作 System.Collections.IList 介面。
PS> 1, 2 -is [System.Collections.IList]
True
-as 算子
運算子 -as
會嘗試將輸入物件轉換成指定的 .NET 類型。 如果成功,則會傳回已轉換的物件。 如果失敗,則會傳 $null
回 。 它不會傳回錯誤。
<input>
如果 是衍生自 .NET 型-as
則會傳遞傳回未變更的輸入物件。 例如, DirectoryInfo 類型衍生自 FileSystemInfo 類型。 因此,在下列範例中,物件類型不會變更:
PS> $fsroot = (Get-Item /) -as [System.IO.FileSystemInfo]
PS> $fsroot.GetType().FullName
System.IO.DirectoryInfo
轉換 DateTime 類型會區分文化特性
不同於類型轉換,使用 [DateTime]
運算符轉換成 -as
類型,只適用於根據目前文化特性規則格式化的字串。
PS> [cultureinfo]::CurrentCulture = 'fr-FR'
PS> '13/5/20' -as [datetime]
mercredi 13 mai 2020 00:00:00
PS> '05/13/20' -as [datetime]
PS> [datetime]'05/13/20'
mercredi 13 mai 2020 00:00:00
PS> [datetime]'13/05/20'
InvalidArgument: Cannot convert value "13/05/20" to type "System.DateTime".
Error: "String '13/05/20' was not recognized as a valid DateTime."
若要尋找 物件的 .NET 類型,請使用 Get-Member
Cmdlet。 或者,使用 所有物件的 GetType 方法,以及 這個方法的 FullName 屬性。 例如,下列語句會取得命令的 Get-Culture
傳回值類型:
PS> (Get-Culture).GetType().FullName
System.Globalization.CultureInfo
範例
下列範例顯示類型運算子的一些用法:
PS> 32 -is [Float]
False
PS> 32 -is "int"
True
PS> (get-date) -is [DateTime]
True
PS> "12/31/2007" -is [DateTime]
False
PS> "12/31/2007" -is [String]
True
PS> (get-process PowerShell)[0] -is [System.Diagnostics.Process]
True
PS> (get-command get-member) -is [System.Management.Automation.CmdletInfo]
True
下列範例顯示當輸入是 物件的集合時,比對類型是集合的 .NET 類型,而不是集合中個別物件的型別。
在此範例中,雖然 和 Get-Culture
Cmdlet 都會Get-UICulture
傳回 System.Globalization.CultureInfo 物件,但這些物件的集合是 System.Object 陣列。
PS> (get-culture) -is [System.Globalization.CultureInfo]
True
PS> (get-uiculture) -is [System.Globalization.CultureInfo]
True
PS> (get-culture), (get-uiculture) -is [System.Globalization.CultureInfo]
False
PS> (get-culture), (get-uiculture) -is [Array]
True
PS> (get-culture), (get-uiculture) | foreach {
$_ -is [System.Globalization.CultureInfo])
}
True
True
PS> (get-culture), (get-uiculture) -is [Object]
True
下列範例示範如何使用 -as
運算符。
PS> "12/31/07" -is [DateTime]
False
PS> "12/31/07" -as [DateTime]
Monday, December 31, 2007 12:00:00 AM
PS> $date = "12/31/07" -as [DateTime]
C:\PS>$a -is [DateTime]
True
PS> 1031 -as [System.Globalization.CultureInfo]
LCID Name DisplayName
---- ---- -----------
1031 de-DE German (Germany)
下列範例顯示當 -as
運算符無法將輸入物件轉換成 .NET 類型時,它會傳 $null
回 。
PS> 1031 -as [System.Diagnostics.Process]
PS>