Friday, November 14, 2008


PowerShell is based on .NET, and makes heavy use of various .NET features including reflection and the underlying .NET Type system. In .NET, and in PowerShell – objects are at the centre of everything. An object always has a type, which contains meta-information about a the object.

With PowerShell, much of the typing happens under the covers – but you can always get information about the object’s type by using the GetType method. Every object (i.e. every PowerShell variable) provides this method and therefore it’s easy to obtain the type information.

To see this in action, consider a variable, call it $I, that we explicitly declare to be an Integer then we set a value to. Using the GetType Method, we can return all the underlying type information – in other words the description of an integer type.

Here’s a bit of PowerShell code that gets and displays type information for $I:

# Declare an integer and assign a value
[int] $i = 42
# Now get type information and print it out
$type = $i.GetType()
$type | fl *

The output from this PowerShell code fragement looks like this:

Module                     : CommonLanguageRuntimeLibrary
Assembly                   : mscorlib, Version=, Culture=neutral, PublicKeyToken=b77a5c561934e089
TypeHandle                 : System.RuntimeTypeHandle
DeclaringMethod            :
BaseType                   : System.ValueType
UnderlyingSystemType       : System.Int32
FullName                   : System.Int32
AssemblyQualifiedName      : System.Int32, mscorlib, Version=, Culture=neutral, PublicKeyToken=b77a5c561934e089
Namespace                  : System
GUID                       : a310fadd-7c33-377c-9d6b-599b0317d7f2
GenericParameterAttributes :
IsGenericTypeDefinition    : False
IsGenericParameter         : False
GenericParameterPosition   :
IsGenericType              : False
ContainsGenericParameters  : False
StructLayoutAttribute      : System.Runtime.InteropServices.StructLayoutAttribute
Name                       : Int32
MemberType                 : TypeInfo
DeclaringType              :
ReflectedType              :
MetadataToken              : 33554626
TypeInitializer            :
IsNested                   : False
Attributes                 : AutoLayout, AnsiClass, Class, Public, SequentialLayout, Sealed, Serializable, BeforeFieldInit
IsVisible                  : True
IsNotPublic                : False
IsPublic                   : True
IsNestedPublic             : False
IsNestedPrivate            : False
IsNestedFamily             : False
IsNestedAssembly           : False
IsNestedFamANDAssem        : False
IsNestedFamORAssem         : False
IsAutoLayout               : False
IsLayoutSequential         : True
IsExplicitLayout           : False
IsClass                    : False
IsInterface                : False
IsValueType                : True
IsAbstract                 : False
IsSealed                   : True
IsEnum                     : False
IsSpecialName              : False
IsImport                   : False
IsSerializable             : True
IsAnsiClass                : True
IsUnicodeClass             : False
IsAutoClass                : False
IsArray                    : False
IsByRef                    : False
IsPointer                  : False
IsPrimitive                : True
IsCOMObject                : False
HasElementType             : False
IsContextful               : False
IsMarshalByRef             : False

This shows the actual type (System.Int), what assembly the type comes from, and a variety of details. Now for most IT Pros, most if not all of this information is not all that exciting. But it can be a great help for both Devs and more dev-friendly IT Pros wanting to understand more about .NET and how to leverage it from PowerShell. The INT32 class is simple, but the type information can help you to understand how this particular type works, something even more important for more complex types.

Technorati Tags: ,

No comments: