So first remember everything in PowerShell is an object. What exactly is an object? It’s something that has some kind of properties. For example, a string can be an object. It has characters, a length along with other items. If you ever need to learn more about an object just remember Get-Member is your friend.

  • Object Property
    • This is exactly how it sounds it’s just a property. Lets say we have a person a property would be their first name, last name, age, height, etc…
  • Object Methods
    • A method is an action or function you can run on an object to perform a task. For example if you have a word you can use a method ToLower() to make the word lowercase.

Let’s look at some examples now. First will look at a string object and pipe it to Get-Member to see it’s properties and methods.

"Matt" | Get-Member

   TypeName: System.String

Name                 MemberType            Definition
----                 ----------            ----------
Clone                Method                System.Object Clone(), System.Object ICloneable.Clone()
CompareTo            Method                int CompareTo(System.Object value), int CompareTo(string strB), int IComparable.CompareTo(System.Object obj), i…
Contains             Method                bool Contains(string value), bool Contains(string value, System.StringComparison comparisonType), bool Contains…
CopyTo               Method                void CopyTo(int sourceIndex, char[] destination, int destinationIndex, int count)
EndsWith             Method                bool EndsWith(string value), bool EndsWith(string value, System.StringComparison comparisonType), bool EndsWith…
EnumerateRunes       Method                System.Text.StringRuneEnumerator EnumerateRunes()
Equals               Method                bool Equals(System.Object obj), bool Equals(string value), bool Equals(string value, System.StringComparison co…
GetEnumerator        Method                System.CharEnumerator GetEnumerator(), System.Collections.IEnumerator IEnumerable.GetEnumerator(), System.Colle…
GetHashCode          Method                int GetHashCode(), int GetHashCode(System.StringComparison comparisonType)
GetPinnableReference Method                System.Char&, System.Private.CoreLib, Version=, Culture=neutral, PublicKeyToken=7cec85d7bea7798e GetPinn…
GetType              Method                type GetType()
GetTypeCode          Method                System.TypeCode GetTypeCode(), System.TypeCode IConvertible.GetTypeCode()
IndexOf              Method                int IndexOf(char value), int IndexOf(char value, int startIndex), int IndexOf(char value, System.StringComparis…
IndexOfAny           Method                int IndexOfAny(char[] anyOf), int IndexOfAny(char[] anyOf, int startIndex), int IndexOfAny(char[] anyOf, int st…
Insert               Method                string Insert(int startIndex, string value)
IsNormalized         Method                bool IsNormalized(), bool IsNormalized(System.Text.NormalizationForm normalizationForm)
LastIndexOf          Method                int LastIndexOf(char value), int LastIndexOf(char value, int startIndex), int LastIndexOf(char value, int start…
LastIndexOfAny       Method                int LastIndexOfAny(char[] anyOf), int LastIndexOfAny(char[] anyOf, int startIndex), int LastIndexOfAny(char[] a…
Normalize            Method                string Normalize(), string Normalize(System.Text.NormalizationForm normalizationForm)
PadLeft              Method                string PadLeft(int totalWidth), string PadLeft(int totalWidth, char paddingChar)
PadRight             Method                string PadRight(int totalWidth), string PadRight(int totalWidth, char paddingChar)
Remove               Method                string Remove(int startIndex, int count), string Remove(int startIndex)
Replace              Method                string Replace(char oldChar, char newChar), string Replace(string oldValue, string newValue), string Replace(st…
Split                Method                string[] Split(char separator, System.StringSplitOptions options), string[] Split(char separator, int count, Sy…
StartsWith           Method                bool StartsWith(string value), bool StartsWith(string value, System.StringComparison comparisonType), bool Star…
Substring            Method                string Substring(int startIndex), string Substring(int startIndex, int length)
ToBoolean            Method                bool IConvertible.ToBoolean(System.IFormatProvider provider)
ToByte               Method                byte IConvertible.ToByte(System.IFormatProvider provider)
ToChar               Method                char IConvertible.ToChar(System.IFormatProvider provider)
ToCharArray          Method                char[] ToCharArray(), char[] ToCharArray(int startIndex, int length)
ToDateTime           Method                datetime IConvertible.ToDateTime(System.IFormatProvider provider)
ToDecimal            Method                decimal IConvertible.ToDecimal(System.IFormatProvider provider)
ToDouble             Method                double IConvertible.ToDouble(System.IFormatProvider provider)
ToInt16              Method                short IConvertible.ToInt16(System.IFormatProvider provider)
ToInt32              Method                int IConvertible.ToInt32(System.IFormatProvider provider)
ToInt64              Method                long IConvertible.ToInt64(System.IFormatProvider provider)
ToLower              Method                string ToLower(), string ToLower(cultureinfo culture)
ToLowerInvariant     Method                string ToLowerInvariant()
ToSByte              Method                sbyte IConvertible.ToSByte(System.IFormatProvider provider)
ToSingle             Method                float IConvertible.ToSingle(System.IFormatProvider provider)
ToString             Method                string ToString(), string ToString(System.IFormatProvider provider), string IConvertible.ToString(System.IForma…
ToType               Method                System.Object IConvertible.ToType(type conversionType, System.IFormatProvider provider)
ToUInt16             Method                ushort IConvertible.ToUInt16(System.IFormatProvider provider)
ToUInt32             Method                uint IConvertible.ToUInt32(System.IFormatProvider provider)
ToUInt64             Method                ulong IConvertible.ToUInt64(System.IFormatProvider provider)
ToUpper              Method                string ToUpper(), string ToUpper(cultureinfo culture)
ToUpperInvariant     Method                string ToUpperInvariant()
Trim                 Method                string Trim(), string Trim(char trimChar), string Trim(Params char[] trimChars)
TrimEnd              Method                string TrimEnd(), string TrimEnd(char trimChar), string TrimEnd(Params char[] trimChars)
TrimStart            Method                string TrimStart(), string TrimStart(char trimChar), string TrimStart(Params char[] trimChars)
Chars                ParameterizedProperty char Chars(int index) {get;}
Length               Property              int Length {get;}

Notice line 3 (above) shows the object type which in this case we have a System.String. This particular objects has lot’s of methods you can run and only 1 property which is length.
Next I’ll show you several ways to view the property along with using some methods.

# Shows the value in the property called Length
Write-Output "Show Lenght"

# This also shows the value of the property leght but notice you can see a column value
Write-Output "Show Lenght using Select-Object"
"Matt" | Select-Object Length

# You can remove the column name with -expandproperty
Write-Output "Show Length using Select-Object -Expandproperty"
"Matt" | Select-Object -ExpandProperty Length

# Use the method ToLower() to make the word all lowercase
Write-Output "Show ToLower() Method"

# Use method ToUpper() to make the word all upper case
Write-Output "Show ToUpper() Method"
Show Lenght
Show Lenght using Select-Object

Show Length using Select-Object -Expandproperty
Show ToLower() Method
Show ToUpper() Method

Hopefully everything is making sense up to this point. How about we have a little fun and make our own object. Let’s use the [pscustomobject] (one of the things I use most in PowerShell beside variables).
Let’s make a person with the following properties.

  • FirstName (string)
  • LastName (string)
  • Age (integer)
  • Vehicle (string)
# Code to make our custom person and store it into $Person variable
$Person = [PSCustomObject]@{
    FirstName = "Alice"
    LastName = "Apple"
    Age = 24
    Vehicle = "Jeep"

# Pipe to Get-Member to see the object type, methods, and properties
$Person | Get-Member
   TypeName: System.Management.Automation.PSCustomObject

Name        MemberType   Definition
----        ----------   ----------
Equals      Method       bool Equals(System.Object obj)
GetHashCode Method       int GetHashCode()
GetType     Method       type GetType()
ToString    Method       string ToString()
Age         NoteProperty int Age=24
FirstName   NoteProperty string FirstName=Alice
LastName    NoteProperty string LastName=Apple
Vehicle     NoteProperty string Vehicle=Jeep

Here you can see we have new type of object System.Management.Automation.PSCustomObject and we can see lines [9-12] are properties. Under the Definition column you can see what types each property value is (string, int).

Let’s say you only cared about seeing the firstName and Age. Simple you can just use Select-Object.

$Person | Select-Object FirstName, Age
FirstName Age
--------- ---
Alice      24

Obviously the methods and properties very from object to object and I don’t have enough time in a week to explain them all. Just remember to use Get-Member. This is a very handy tool. Also, remember EVERYTHING in PowerShell is an object! Event that output from Get-Member is one. Yep that’s right! I found in the past when I need to know what properties are there on an object in my scripts to make the Dynamic. I use this little trick below.
I’ll take the object $person | pipe it to Get-Member then I’ll pipe that output to Where the MemberType column is a property.

$Person | Get-Member | Where-Object {$_.MemberType -like "*Property*"}
   TypeName: System.Management.Automation.PSCustomObject

Name      MemberType   Definition
----      ----------   ----------
Age       NoteProperty int Age=24
FirstName NoteProperty string FirstName=Alice
LastName  NoteProperty string LastName=Apple
Vehicle   NoteProperty string Vehicle=Jeep

Notice the output above only shows the items where the MemberType Column contains the word *property*. All you have to do now is Select-Object pick the Name column and expand it.

$Person | Get-Member | Where-Object {$_.MemberType -like "*Property*"} | Select-Object -ExpandProperty Name

Pretty cool right? I hope you got some value from this post. If you have any questions or like to see some more examples let me know.

Leave a Reply

Your email address will not be published. Required fields are marked *

Close Bitnami banner