Getting the Object Type in PowerShell
First post of the year and in a while and I would like to talk about the types of the objects in PowerShell.
Much like C#, each object has a type in PowerShell. This type is proven to be very helpful when developing functions and cmdlets since it provides information about the object we are dealing with.
For example you may have a variable that holds the value "1" but it can be of type string, integer, double and others. The way to differentiate is to examine the type of the variable.
There are may ways to get the type of a PowerShell object but first, let's get the newest event in the application log to use as example:
The first way to get the type of the object is to use the "GetType" method of the object. This method exists on all objects since it is inherited from the basic object type.
Than name of the type will be in the "Name" property. Furthermore, the "FullName" property of the returning object will provide the full name of the type.
The second way is to use the Get-TypeData cmdlet like the following:
The "TypeName" property will contain the full name of the type.
Last but not least, the good old "Get-Member" function will display the full name of the type:
You can always check TechNet and MSDN for an object type and see it's properties and methods!
Much like C#, each object has a type in PowerShell. This type is proven to be very helpful when developing functions and cmdlets since it provides information about the object we are dealing with.
For example you may have a variable that holds the value "1" but it can be of type string, integer, double and others. The way to differentiate is to examine the type of the variable.
There are may ways to get the type of a PowerShell object but first, let's get the newest event in the application log to use as example:
PS C:\> $obj = Get-EventLog -LogName Application -Newest 1
PS C:\> $obj
Index Time EntryType Source InstanceID Message
----- ---- --------- ------ ---------- -------
3882 Jan 07 20:39 Information ESENT 916 svchost (3356,G,0) The beta ...
PS C:\> $obj
Index Time EntryType Source InstanceID Message
----- ---- --------- ------ ---------- -------
3882 Jan 07 20:39 Information ESENT 916 svchost (3356,G,0) The beta ...
The first way to get the type of the object is to use the "GetType" method of the object. This method exists on all objects since it is inherited from the basic object type.
PS C:\> $obj.GetType()
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True Event LogEntry System.ComponentModel.Component
PS C:\> $obj.GetType().FullName
System.Diagnostics.EventLogEntry
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True Event LogEntry System.ComponentModel.Component
PS C:\> $obj.GetType().FullName
System.Diagnostics.EventLogEntry
Than name of the type will be in the "Name" property. Furthermore, the "FullName" property of the returning object will provide the full name of the type.
The second way is to use the Get-TypeData cmdlet like the following:
PS C:\> $obj | Get-TypeData
TypeName Members
-------- -------
System.Diagnostics.EventLogEntry {[EventID,System....}
TypeName Members
-------- -------
System.Diagnostics.EventLogEntry {[EventID,System....}
The "TypeName" property will contain the full name of the type.
Last but not least, the good old "Get-Member" function will display the full name of the type:
PS C:\> $obj | Get-Member
TypeName: System.Diagnostics.EventLogEntry#Application/ESENT/916
Name MemberType Definition
---- ---------- ----------
TypeName: System.Diagnostics.EventLogEntry#Application/ESENT/916
Name MemberType Definition
---- ---------- ----------
You can always check TechNet and MSDN for an object type and see it's properties and methods!