Topic: Get-Member Continued
Notice: This post is a part of the PowerShell Monday series — a group of quick and easy to read mini lessons that briefly cover beginning and intermediate PowerShell topics. As a PowerShell enthusiast, this seemed like a beneficial way to ensure those around me at work were consistently learning new things about Windows PowerShell. At some point, I decided I would share these posts here, as well. Here’s the PowerShell Monday Table of Contents.
If nearly everything in PowerShell is an object, then let’s look at two of the members — properties and methods — of our two results, ’55’ and 10. Typically, we pipe to Get-Member. In the first example, we’ll recreate our string value of ’55’ and see what we can learn about it.
$x = '5' + '5' $x 55 $x | Get-Member TypeName: System.String
In the above results, I’ve included only the TypeName of the object for now, and haven’t yet included all the properties and methods returned by Get-Member, that we’ll see momentarily. The TypeName indicates that the object is a string object.
In the more complete, Get-Member results, I’ve included the first several methods and the one and only property. As the value in $x is a string, there’s not much to it, property wise. In this case, there’s just a length property. In the below results, I haven’t include the Definition property. If you run these commands yourself, don’t be surprised to see a third property, or column, included.
TypeName: System.String Name MemberType ---- ---------- Clone Method CompareTo Method Contains Method CopyTo Method EndsWith Method Equals Method GetEnumerator Method GetHashCode Method ... Length Property
Let’s return the length property to see the length of the string stored in the variable $x. The value is ’55’, so a length of two would make sense. Let’s double-check.
$x.Length 2
Let’s recreate our numeric value of 55, as well, and pipe it to Get-Member.
$y = 5 + 5 $y 10 $y | Get-Member TypeName: System.Int32 Name MemberType ---- ---------- CompareTo Method Equals Method GetHashCode Method GetType Method GetTypeCode Method ToBoolean Method ToByte Method ToChar Method ToDateTime Method ToDecimal Method ToDouble Method ToInt16 Method ToInt32 Method ToInt64 Method ToSByte Method ToSingle Method ToString Method ToType Method ToUInt16 Method ToUInt32 Method ToUInt64 Method
The variable $y is storing an integer object, and according to Get-Member, doesn’t include any properties. That’s a good start for this holiday Monday. Next week, we’ll start working with some of the different methods.