This section describes the type operators in Powershell.
type operators in Powershell can be used in conjunction with the. NET Framework type to determine whether an object is an instance of the specified. NET Framework type, or whether an object can be converted to. Net The Framework type.
Powershell supports the following type operators
Operator
|
Description
|
Example
|
-is |
Returns TRUE if the object is the specified. Net Framework type, otherwise FALSE |
PS c:\> "abc"-is [string] True |
-isnot |
-is the opposite of the operator, returns TRUE if the object is not the specified. Net Framework type, otherwise returns FALSE |
PS c:\> "abc"-isnot [string] False |
-as
|
Converts an object to the specified. Net Framework type. If the conversion succeeds, the converted object is returned. If the conversion fails, nothing is returned (and no error is returned)
|
PS c:\> "1234"-as [int] 1234 PS c:\> "abc"-as [int] PS c:\> |
The syntax for the type operator is as follows:
<input> <operator> [. NET type]
. Net type can also be placed in double quotation marks, that is, you can also use the following syntax:
<input> <operator> ". NET type"
For the System.DateTime type, you can enter [datetime] or DateTime. If a type is not in the root namespace of the system, you need to specify the full name of the object type. "System." Command space can be omitted. For example, to specify System.Diagnostics.Process,
You can enter [System.Diagnostics.Process], [diagnostics.process], or "diagnostics.process".
The type operator always returns a Boolean value even when the input object is a collection. However, if the input is a collection, the type operator will match the. NET Framework type of this collection. The type operator does not match the type of each object , even if all objects are of the same type.
To find the. NET Framework type of an object, use the object through the pipe operator (|) Connect the Get-member cmdlet. Alternatively, you can use the GetType () method of all objects together with the FullName property of the method, which is described in the types of variables in PowerShell.
The following examples illustrate the use of these operators
Examples of usages of the-is operator
PS c:\> 3-is [float]falseps c:\> 3-is [int]trueps c:\> (get-date)-is [datetime]trueps C:\> "20170111"-is [DateTime] Falseps c:\> "01/11/2017"-is [datetime]falseps c:\> "01/11/2017"-is [string]trueps C:\> (Get-Process Powershel L)-is [system.diagnostics.process]trueps c:\> (Get-command get-member)-is [ System.management.automation.cmdletinfo]true
The following example shows that if the input object is a collection, the type of the. Net Framework of the collection is judged, not the type of a single object in the collection.
PS c:\> (get-culture)-is [system.globalization.cultureinfo]trueps c:\> (get-uiculture)-is [ System.globalization.cultureinfo]trueps c:\> (Get-culture), (get-uiculture)-is [ System.globalization.cultureinfo]falseps c:\> (Get-culture), (get-uiculture)-is [Array]trueps C:\> ( Get-culture), (get-uiculture) | Foreach {$_-is [system.globalization.cultureinfo]}truetrueps c:\> (Get-culture), (get-uiculture)-is [Object]True
In the example above, although both the Get-culture and Get-uiculture cmdlets return system.globalization
. CultureInfo objects, but the collection of these objects is a System.Object array.
2. Examples of usages of the-as operator
ps c:\> "01/11/2017" -is [datetime]falseps c:\ > "01/11/2017" -as [datetime]2017 January 11 Wed 00:00:00PS C:\> $date = "01/11/2017" -as [datetime]PS C:\> $date -is [datetime]trueps c:\ > 1031 -as [System.Globalization.CultureInfo]LCID Name displayname---- ---- -----------1031 de-DE German (Germany)
Also mentioned above, when the input object with the-as operator cannot be converted to the specified. Net Framework type, nothing is output. For example:
PS c:\> "Shell"-as [Int]ps c:\>
Summarize
-is,-isnot Determines whether an object is an instance of the specified. Net Framework, or is used to determine whether the specified file is a file or a folder. The-as operator is used for type conversions, such as when a database queries to a string time and converts it to a [DateTime] type.
This article from "Flower Blossom Fall" blog, declined reprint!
(15) Type operators in PowerShell