Introduction
When I started writing this article, I wasn’t entirely sure where to go with what I wanted to cover. This is the first time I have explored these methods and properties in this manner. Normally when writing scripts, I utilize what I have picked up over time to process data or extract data for my current needs. However, I try to learn something new about PowerShell every day, and this time it was no different. The intent of this blog article is to bring what I’ve found to light and propose some practical uses for the information as well as how to dig in more to find even more use cases for these methods.
More Information
When we think about variables, we think of them as convenient tools for storing data or information that can be used later, exported to a file, etc. However, did you know that when something is placed in a variable, that there are other aspects, behind the scenes, that are also enabled depending on the data type that is stored in that variable? These aspects are known as Methods (or actions) and Properties (both variable and object properties). We can expose these aspects which then provide powerful tools that we can use to manipulate variables and their data.
How to Reveal?
In order to expose these values, we can use one of two methods. One is to use a ‘.’ after the variable name, then use ‘Ctrl+space’ to expose the values or we can add a ‘ | Get-Member’ right after the variable. Let’s walk through each method and see what they expose.
Variable and “.”
What does the ‘.’ expose? It exposes properties/methods of the variable itself. Not the properties and methods of the data itself. For example, we have this for a variable where properties (Length, count, etc.) and also methods (Split, Replace, etc.):
Using Get-Member – shows methods
If we’ve retrieved a list of mailboxes and stored object information in a $Mailboxes variable, we are able to expose details about properties and methods as shown below:
In this one we have a long list (actually the screenshot is truncated) which lists all the Methods and properties contained in the variable. The pros to this are that we get a detailed list of what is a Method and what is a Property, along with a ‘Definition field) which provides some context to the data. The cons to this are that the list is not compact and we cannot select an item right from the list, but must type in the variable name again with the property or method we want to use.
Get-Member vs Variable “.”
Decimal Variable
Let’s start with an easy variable to see what we can reveal with these two options:
$Count = 0 $Count++
What is available with this variable:
$Count | Get-Member
String Variable
$TestPhrase = 'This is a test phrase.'
$TestPhrase | Get-Member
Provides the same list, but in this format:
Array Variable
$Words = 'test','test2','test3'
$Words | Get-Member
Notice that there are more options available to be used:
Array Variable 2
$Files = Get-ChildItem
Dot results:
Get-Member results: (two columns are to show the totality of Methods, properties and more)
Array Variable 3
$Mailboxes = Get-Mailbox
$Mailbox ‘dot method’ reveal this:
$Mailboxes | Get-Member
Notice in this instance we have properties of the objects ( a.k.a. NoteProperty ) that are stored in the variable as well.
Practical Examples
While not all methods or properties are the same, most can be operated with a ‘.’
Let’s explore a couple of ‘.’ examples to get an idea how we can use them to our advantage. First, with this variable, what do we have available:
Below are some examples with an option picked from the above list.
$Mailboxes.Count
$Mailboxes.Length
$Mailboxes.IsReadOnly
Count and length are a measure of the number of objects, or mailboxes in this case, that are stored in this variable. While isReadOnly is a check to see if the variable has been marked as Read Only or not. One Interesting one, and be careful as it is destructive, is to use the Clear action which removes everything from the variable:
$Mailboxes.Clear
Let’s assume that we want to trim some information stored in a variable, we can use the self complete method present in PowerShell to see what options are available and how to use them.
$Words.Tr
We see these results:
If we hit tab (or use the arrow keys), the next option ‘TrimEnd’, will reveal the next option:
Or
Empty Variable
If there are no objects in the variable, we cannot check for properties of an empty variable:
Further Reading
Detailed Information on Methods
Detailed information on Properties
Foreach and where magic methods
Conclusion
Even with the multitude of options and properties, what is exposed here is not comprehensive and does not cover every action we can take on these variables. For example not every variable exposes the Split and Replace methods, but they still may be useable, and this means that some experimentation may be in order to find available actions. With these unknowns, the takeaway from this post should be the fact that experimentation is a necessary part of working with PowerShell. Explore the options, try them out on different data types – strings, arrays, etc. – to see how they operate and use this knowledge to build solutions with PowerShell.
———————————————————————————————————–
Comments? Questions?
Feel free to leave your Comments below! Learn to more efficiently utilize PowerShell to manage Exchange Server, Exchange Online, Microsoft Defender for Office or Microsoft Purview Compliance portals by picking up frequently updated eBooks: