about_Type_Operators

適用対象: Windows PowerShell 2.0, Windows PowerShell 3.0, Windows PowerShell 4.0, Windows PowerShell 5.0

トピック

about_Type_Operators

概要

Microsoft .NET Framework 型を扱う演算子について説明します。

詳細説明

ブール型の演算子 (-is と -isNot) は、オブジェクトが指定された .NET Framework 型のインスタンスであるかどうかを判断します。-is 演算子は、型が一致する場合は TRUE の値を返し、一致しない場合は FALSE の値を返します。-isNot 演算子は、型が一致する場合は FALSE の値を返し、一致しない場合は TRUE の値を返します。

-as 演算子は、入力オブジェクトを指定された .NET Framework 型に変換しようとします。成功すると、変換されたオブジェクトが返されます。失敗すると、何も返されません。この演算子はエラーを返しません。

次の表は、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
              .NET Framework 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.

型演算子の構文は次のとおりです。

        <input> <operator> [.NET type]

次の構文を使用することもできます。

        <input> <operator> ".NET type"

.NET Framework 型を指定するには、型名を角かっこ ([ ]) で囲むか、System.DateTime の [DateTime] や "DateTime" などの文字列として型を入力します。型がシステム名前空間のルートにない場合、オブジェクト型のフルネームを指定します。"System." は省略できます。たとえば、System.Diagnostics.Process を指定するには、[System.Diagnostics.Process]、[Diagnostics.Process]、または "diagnostics.process" と入力します。

型演算子は、入力がオブジェクトのコレクションであっても、常にブール値を返します。ただし、入力がコレクションの場合、型演算子はコレクションの .NET Framework 型に一致します。型演算子は、すべてのオブジェクトが同じ型であっても、それぞれのオブジェクトの型には一致しません。

オブジェクトの .NET Framework 型を調べるには、Get-Member コマンドレットを使用します。または、すべてのオブジェクトの GetType メソッドをこのメソッドの FullName プロパティと共に使用します。たとえば、次のステートメントは Get-Culture コマンドの戻り値の型を取得します。ここにセクション本体を挿入します。

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

次の例では、型演算子の使用例をいくつか示します。

        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

次の例では、入力がオブジェクトのコレクションである場合、一致する型はコレクションの .NET Framework 型であり、コレクション中の個々のオブジェクトの型ではないことを示します。

この例では、Get-Culture コマンドレットと Get-UICulture コマンドレットはいずれも System.Globalization.CultureInfo オブジェクトを返しますが、これらのオブジェクトのコレクションは System.Object 配列です。

        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

次の例では、-as 演算子の使用方法について示します。

        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)

次の例では、-as 演算子が入力オブジェクトを .NET Framework 型に変換できない場合、何も返さないことを示します。

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

関連項目

about_Operators