Powershell typeof

Coming from C#, it provides the built in function typeof() that you can use to get the Type of a class. Powershell also makes it easy to get Type information.

Wrapping the type in square brackets [TypeName] evaluates to an instance of System.Reflection.TypeInfo. Which you can then use to compare it as per normal.

PS>"".GetType() -Eq [string]
PS>(1).GetType() -Eq [int]
PS>(1.0).GetType() -Eq [int]

You can also assign the value to a variable if needed.

PS>$x = [bool]

IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     True     Boolean                                  System.ValueType

I hope this will make it easier to find next time!

Daniel LittleWritten by Daniel Little