Converting VBScript's Vartype Function

Windows PowerShell

Definition: Returns a value indicating the subtype of a variable.


In VBScript you can use the VarType function to return an integer value that represents a particular data type (for example, a 9 represents an object). To be honest, we don’t know how to do this in Windows PowerShell (that is, return an integer value as opposed to the actual data type name). And, to be equally honest, we don’t think it matters much.

In case you’re curious, VBScript uses a data type known as a Variant. That means that its types are comprised of 2 things: a chunk of memory where the type value is stored, and a description of the type. If you have a set of bits stored in memory somewhere, that’s not enough to tell where it’s a number 32 or the space character or a certain object with certain values. Because of that VBScript’s VarType function returns an integer that tells you the type bit for the Variant.

PowerShell variables are strongly typed. That means there is no exact equivalent for the VarType function. To determine the data type in a PowerShell variable you pipe the contents of the variable to Get-Member and rely on the Cmdlet to tell you the type. There’s no way to have an enumeration that maps to each possible data type; because you can construct new data types on the fly, that enumeration would have to account for an infinite number of data types. Needless to say, that would be a pretty tough job.

Return to the VBScript to Windows PowerShell home page