7

[Edit after both answers said lack of clarity] My Goal: Perform different operations depending if what is passed to a function is an "object" type thing (e.g. an array, a hashtable) or just a simple "string" type thing. If it's just a string, I'll simply include it in the body of the email. If it's an array or hashtable, I need to do a bunch of processing on it to convert it into an HTML table.

[Original Question Text] I'm passing a variable $body to a function Email-Report that can either be a simple string or an object (e.g. a hashtable or array). I want to check if $body is an object and do different things depending. My problem is $body could be almost anything, not just a string or a hashtable. So I can't just check if $body.GetType().Name -eq String

I've tried $body.GetType().Name which returns

IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     True     Hashtable                                System.Object

However if the variable is an array BaseType becomes System.Array so I can't filter on that property, also as mentioned above the $body variable may not always be a hashtable or an array. If it's a hash table, $var.GetType() returns a BaseType of System.Object, however I can't seem to refer to the BaseType property. ($hash.GetType()).BaseType returns another object itself, which itself has a BaseType property which is blank.

I've also tried $body.IsObject and $body.IsObject() but those methods don't seem to exist. I've also tried $body -eq [System.Object] which I expected to be $true, but it returns $false.

Not sure where to go from here - I think I'm missing something obvious or having a logic error.

2
  • 2
    The BaseType will tell you what the current object is inheriting from. Therefore, the base for hashtable and string would be system.object. What is your goal? Is it to see if the main type is System.Object? do you want to just see if it is a string, array, or hashtable?
    – appsecguy
    Jul 5, 2019 at 2:15
  • I wanted to see if a variable is a string, or an object. However I now understand from the two answers below that even strings are considered objects in Powershell. I'll try edit my question for clarity if I can see a way of doing so which won't affect the general gist that people have already answered.
    – Gostega
    Jul 8, 2019 at 0:42

2 Answers 2

19

It's not entirely clear from the question what your goal or motivation is here, but here goes:

Every single object in PowerShell ultimately inherits from System.Object due to the nature of .NET's type system, so it's a bit of a silly test to attempt to employ type identity comparison for, since you could simply do:

function Test-IsObject
{
    param(
        [AllowNull()]
        $InputObject
    )

    return $null -ne $InputObject
}

If you want to test that the the object in question is not a value type (ie. not a struct or an integral type, but a class), inspect the IsValueType property of the type:

function Test-IsRefType
{
    param(
        [AllowNull()]
        $InputObject
    )

    return ($null -ne $InputObject -and -not $InputObject.GetType().IsValueType)
}

If you want a generalized solution to test whether a certain type occurs in the type hierarchy of an object, there are three general approaches:

  1. Rely on PSTypeNames
  2. Use the -is operator
  3. Resolve all base types yourself

PSTypeNames

All objects in PowerShell have a special property called PSTypeNames that contains the type names of all types in the type hierarchy for the underlying object + (optionally) PowerShell-defined type extension names - this is how PowerShell differentiates formatting of instances of different CIM classes for example.

Since PSTypeName can be manipulated by the user directly, this is inherently "unsafe", but will work in most cases:

function Test-IsType
{
    param(
        [object]$InputObject,
        [string]$TypeName
    )

    return $InputObject.PSTypeNames -contains $TypeName 
}

Builtin type operator(s)

Since PowerShell 3.0 we have two new type operators: -is and its negated counterpart -isnot. These actually inspect the runtime type of the underlying .NET object, so they're safer than inspecting the synthetic PSTypeNames property:

$Object -is [System.Object]   # $true for any value assigned to $Object
"" -is [string]               # $true
5 -is [int]                   # $true

-is automatically tests for base types and interfaces as well (all statements below are $true):

$strings = 'a', 'b', 'c' -as [string[]]
$strings -is [array]
$strings -is [System.Collections.Generic.IEnumerable[string]]
$strings -is [object]

These operators, along with the related -as operator, are documented in the about_Type_Operators help topic.

Resolve type hierarchy manually

Finally, if we want to explore a little further we can resolve the type hierarchy manually by simply dereferencing GetType().BaseType until we hit System.Object. Below is a simple helper function that emits all the base types, which we can then compare against:

function Get-BaseType
{
    param(
        [type]$Type,
        [switch]$IncludeLeaf
    )

    if($IncludeLeaf){
        # We're "walking backwards", so we'll start by emitting the type itself
        $Type
    }

    # Now go through the BaseType references
    # At some point we'll reach System.Object and (BaseType -eq $null) 
    while($BaseType = $Type.BaseType){
        ($Type = $BaseType)
    }
}

function Test-IsType
{
    param(
        [object]$InputObject,
        [type]$TypeName
    )

    return $TypeName -in (Get-BaseType -Type $InputObject.GetType() -IncludeLeaf)
}

Note that you could just use -is instead of Test-IsType, except if you specifically wanted to test for base classes only, not also for interfaces.

2
  • 1
    I've also opened a docs issue on GitHub to ask for improvements to about_Type_Operators: github.com/MicrosoftDocs/PowerShell-Docs/issues/4524
    – mklement0
    Jul 7, 2019 at 15:25
  • Thanks, both answers were very similar. Both helpful and I learned that almost all variable in Powershell are considered objects. You gave me the technique to check if a variable is a string using "" -is [string] hence I mark it as answer.
    – Gostega
    Jul 8, 2019 at 1:45
10

If you are just wanting to test an object's type, you can use the -is operator to compare a variable or value to a type.

# String test

PS > $str = "a string"
PS > $str -is [String]
True

PS > $str -is [Int]
False

PS > $str.gettype().Name
String

# Array Test

PS > $arr = @(1,2,3)
PS > $arr.GetType().Name
Object[]

PS > $arr -is [Object[]]
True

# Hashtable Test

PS > $hash = @{property='Value'}
PS > $hash.GetType().Name
Hashtable
PS > $hash -is [Hashtable]
True
PS > $hash -is [Object[]]
False
PS > $hash -is [String]
False

Almost any variable you assign will be an object or reference to an object. So testing anything as a type [object] will almost always be True.

To be safer though, you should rely on a type's full name when doing a type comparison because not all types have a type accelerator. Take the ArrayList type below as an example. The type name may be ArrayList, but since there is no type accelerator called [ArrayList] AND it is not directly off of the System namespace, the check will throw an error without the full type name. You can always leave off System, i.e. [String] is the same as [System.String].

# Bad ArrayList Test

PS > $e = @(1,2) -as [Collections.ArrayList]
PS > $e.GetType().Name
ArrayList
PS > $e -is [ArrayList]
Unable to find type [ArrayList].
At line:1 char:8

# Good Arraylist Test

PS C:\temp\test1> $e.GetType().FullName
System.Collections.ArrayList
PS > $e -is [System.Collections.ArrayList]
True

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.