PowerShell Properties and SubProperties


For my second post of the year, I am posting a tip I wrote a few months back and forgot to post. Enjoy!

Introduction

In this edition of PowerShell Tip of the Week, we get back to business and take a look at object properties and how to get sub-properties and possibly even sub-sub-properties of an object … Let’s define a couple of words for clarity in this article.

Object: For this tip, is simply an item we wish to examine with PowerShell. This could be a computer object, a mailbox or some sort of policy we have configured with PowerShell.
Property: is an attribute associated with an object. For example, the PrimarySMTPAddress of a mailbox.

Example 1

For our first example let’s look over Active Directory Sites Links and see what we can glean from them in terms of properties. [To perform this, make sure you run these cmdlets on a Windows 2016 or newer domain controller]
[sourcecode language=”powershell”]
Get-ADSiteLink
[/sourcecode]

Here we have a list of sites, with their respective costs and linked sites. If we need to verify the sites in a Site Link, then we can query each site link again and pull just the ‘Sites’ property:
[sourcecode language=”powershell”]
(Get-AdSiteLink).Sites | Ft
[/sourcecode]

Example 2

Next, we will look at the properties of a mailbox in Exchange Server 201x. The cmdlet we need is Get-Mailbox.
[sourcecode language=”powershell”]
Get-Mailbox Administrator
[/sourcecode]

We see there are four properties that are exposed. All of these are simple properties with no sub-properties. However, the PrimarySMTPAddress of a mailbox will have sub-properties that we can expose. First, we will look at the property by itself:
[sourcecode language=”powershell”]
Get-Mailbox Administrator | ft PrimarySmtpAddress
[/sourcecode]

We see what appears to be a simple email address. However, if we grab just the property itself a different way, by encapsulating the object in brackets and then requesting just one property (‘.primarysmtpaddress’), then we see the property is not so simple:


In fact we see that the PrimarySMTPAddress has six properties. The important ones are local, domain and address. Local and Domain are the parts of a mailbox’s Primary SMTP Address that are left and right of the ‘@’ symbol respectively. Let’s just choose the Address value and store it in a variable:


Sometimes it requires using Format List to expose extra values or properties with PowerShell, especially if the above method does not work.

Conclusion

As we can see from the examples above, items in PowerShell have properties and sub-properties that we can expose and work with. These can be important in your scripts when pulling data, manipulating data or looking for an attribute to confirm its value.

FURTHER READING

I cover this and many more topics in this book:

Notes from the Field: A PowerShell Primer

Related Post