Describes the operators that work with Microsoft .NET Framework types. (about_type_operators)

   
# TOPIC
about_Type_Operators

# SHORT DESCRIPTION
Describes the operators that work with Microsoft .NET Framework types.

# LONG DESCRIPTION
The Boolean type operators (-is and -isnot) tell whether an object is an
instance of a specified .NET Framework type. The -is operator returns a
value of TRUE if the type matches and a value of FALSE otherwise.
The -isnot operator returns a value of FALSE if the type
matches and a value of TRUE otherwise.

The -as operator tries to convert the input object to the specified .NET
Framework type. If it succeeds, it returns the converted object. It if
fails, it returns nothing. It does not return an error.

The following table lists the type operators in Windows PowerShell.

Operator Description Example
-------- ------------------------ -------------------------------------
-is Returns TRUE when the C:\PS> (get-date) -is [datetime]
input is an instance True
of the specified
.NET Framework type.

-isnot Returns TRUE when the C:\PS> (get-date) -isnot [datetime]
input is not an instance False
of the specified
.NETFramework type.

-as Converts the input to C:\PS> 12/31/07 -as [datetime]
the specified Monday, December 31, 2007 12:00:00 AM
.NET Framework type.

The syntax of the type operators is as follows:

<input> <operator> [.NET type]

You can also use the following syntax:

<input> <operator> ".NET type"

To specify the .NET Framework type, enclose the type name in
brackets ([ ]), or enter the type as a string, such as [DateTime] or
"datetime" for System.DateTime. If the type is not at the root of the
system namespace, specify the full name of the object type. You can omit
"System.". For example, to specify System.Diagnostics.Process, enter
[System.Diagnostics.Process], [Diagnostics.Process], or
"diagnostics.process".

The type operators always return a Boolean value, even if the input is a
collection of objects. However, when the input is a collection, the type
operators match the .NET Framework type of the collection. They do not
match the type of each object, even when all of the objects are of the
same type.

To find the .NET Framework type of an object, use the Get-Member cmdlet.
Or, use the GetType method of all the objects together with the FullName
property of this method. For example, the following statement gets the
type of the return value of a Get-Culture command:

C:\PS> (get-culture).gettype().fullname
System.Globalization.CultureInfo

EXAMPLES
The following examples show some uses of the Type operators:

C:\PS> 32 -is [Float]
False

C:\PS> 32 -is "int"
True

C:\PS> (get-date) -is [DateTime]
True

C:\PS> "12/31/2007" -is [DateTime]
False

C:\PS> "12/31/2007" -is [String]
True

C:\PS> (get-process powershell)[0] -is [System.Diagnostics.Process]
True

C:\PS> (get-command get-member) -is [System.Management.Automation.CmdletInfo]
True

The following example shows that when the input is a collection of objects,
the matching type is the .NET Framework type of the collection, not the type
of the individual objects in the collection.

In this example, although both the Get-Culture and Get-UICulture cmdlets
return System.Globalization.CultureInfo objects, a collection of these
objects is a System.Object array.

C:\PS> (get-culture) -is [System.Globalization.CultureInfo]
True

C:\PS> (get-uiculture) -is [System.Globalization.CultureInfo]
True

C:\PS> (get-culture), (get-uiculture) -is [System.Globalization.CultureInfo]
False

C:\PS> (get-culture), (get-uiculture) -is [Array]
True

C:\PS> (get-culture), (get-uiculture) | foreach {$_ -is [System.Globalization.CultureInfo])
True
True

C:\PS> (get-culture), (get-uiculture) -is [Object]
True

The following examples show how to use the -as operator.

C:\PS> "12/31/07" -is [datetime]
False

C:\PS> "12/31/07" -as [datetime]
Monday, December 31, 2007 12:00:00 AM

C:\PS> $date = "12/31/07" -as [datetime]

C:\PS>$a -is [datetime]
True

C:\PS> 1031 -as [System.Globalization.CultureInfo]

LCID Name DisplayName
---- ---- -----------
1031 de-DE German (Germany)

The following example shows that when the -as operator cannot convert the
input object to the .NET Framework type, it returns nothing.

C:\PS> 1031 -as [System.Diagnostic.Process]
C:\PS>

SEE ALSO
about_Operators

C:\Windows>powershell get-help about_Variables -full

ColorConsole [Version 1.7.1000] PowerShell 2.0-Export
Microsoft Windows [Version 6.1.7600]
Copyright (c) 2014 Microsoft Corporation.

OS: Windows 10, 8.1, 8, 7, Vista / Windows Server 2016, 2012, 2008
»»»» ColorConsole





... Home  
... Impressum
... ColorConsole

... CMD
... Netsh-XP
... Netsh-7
... Netsh-Vista
... Powershell










Download the Windows 10 Starter, Home (Basic Premium), Professional und Ultimate!

... facebook.de
HTTP: ... PS_Windows/en/about_type_operators.htm
0.078

Fastest way to switch user in Windows 10 without log-off?

 /

Create a password reset eg. recovery disk for Windows 8.1, 8 and Windows 10!

 /

Extend test period in Windows 7 (Delay activation)!

 /

Was ist Windows Vista-Aero?

 /