Daniel Little Coding Blog
Daniel Little Coding Blog

A blog about .net development and the web.

Daniel Little
Author

Daniel Little (lavinski) is a Brisbane based software developer.

Share


Our Newsletter


Like this blog? Get each new post as soon it's posted.

Tags


Twitter


Powershell typeof

Daniel LittleDaniel Little

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]
True
PS>(1).GetType() -Eq [int]
True
PS>(1.0).GetType() -Eq [int]
False

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

PS>$x = [bool]
PS>$x

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

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

Daniel Little
Author

Daniel Little

Daniel Little (lavinski) is a Brisbane based software developer.

Comments