How to Use PowerShell Calculated Properties

PowerShell is an object-oriented programming (OOP) shell and scripting language. That means every PowerShell cmdlet you run produces an object containing the cmdlet’s output in a structured format. Objects can be passed among cmdlets and the data retrieved by calling properties. OOP is much easier than parsing strings of text to extract the information you need.

But sometimes the data stored in objects isn’t in the format we need, or we need to add information that is missing so that the data can be processed further. PowerShell calculated properties let you change the data stored in objects or add new data. In this article, we’re going to look at a practical example that uses a short script to generates a report on changes to the Active Directory (AD) schema. In the script, we’ll use a calculated property to change the format used to display a date.

Listing Active Directory schema changes using PowerShell

The script starts by using Get-ADObject to search for changes to the AD schema. We will use three parameters with the cmdlet. -SearchBase specifies the AD path to search. Get-ADRootDSE automatically finds the root path for the domain’s schema, which should look something like what you can see below if the domain name is ad.globomantics.uk:


Sponsored Content

What is “Inside Microsoft Teams”?

“Inside Microsoft Teams” is a webcast series, now in Season 4 for IT pros hosted by Microsoft Product Manager, Stephen Rose. Stephen & his guests comprised of customers, partners, and real-world experts share best practices of planning, deploying, adopting, managing, and securing Teams. You can watch any episode at your convenience, find resources, blogs, reviews of accessories certified for Teams, bonus clips, and information regarding upcoming live broadcasts. Our next episode, “Polaris Inc., and Microsoft Teams- Reinventing how we work and play” will be airing on Oct. 28th from 10-11am PST.

-SearchScope specifies the scope of the search. We’ll use OneLevel to search the immediate children of the path specified in -SearchBase. We want to search for everything, so -Filter is set to *. And finally, -Properties is used to specify which properties we’re interested in storing in the object, so we’ll store objectClass, name, whenChanged, and whenCreated.

$schema = Get-ADObject -SearchBase ((Get-ADRootDSE).schemaNamingContext) -SearchScope OneLevel -Filter * -Properties objectClass, name, whenChanged, whenCreated | Select-Object objectClass, name, whenChanged, whenCreated, @{Name='Event'; Expression={($_.whenCreated).Date.ToShortDateString()}} | Sort-Object -Property whenCreated

$schema | Format-Table objectClass, name, whenChanged, whenCreated -GroupBy Event -Autosize

Using Select-Object to create a new object with a calculated property

We’ll then ‘pipe’ the object created by Get-ADObject to Select-Object, which we’ll use to create a new object with a slightly modified whenCreated property. The objectClass, name, and whenChanged properties we’ll bring across unchanged. But whenCreated we’ll transform to use a short date format and then use it later to group the schema changes in the final output.

Calculated properties take on the format below. The @ symbol followed by a script block:

@{ Name = '';  Expression = {}}

Name is used to give the calculated property a name and Expression is used to transform or create new data. In this example, we’ll call the calculated property ‘Event’ and take just the date, leaving behind the time, from whenCreated and transform it to short date format using ToShortDateString.
@{Name='Event'; Expression={($_.whenCreated).Date.ToShortDateString()}}

Sort and format object data

Before we deal with formatting the final output, we’ll pipe the object created by Select-Object to the Sort-Object cmdlet to sort schema changes using the whenCreated property. Finally, we can take the resulting object, which is stored in the $schema variable, and pipe it to Format-Table.

$schema | Format-Table objectClass, name, whenChanged, whenCreated -GroupBy Event -Autosize

Here we are using Format-Table to list the properties in the order we want the columns of the table to appear. The -GroupBy parameter is used to group the schema events by date using our calculated property.

Image #1 Expand
How to Use PowerShell Calculated Properties (Image Credit: Russell Smith)


Let’s run the two lines of code together and look at the output (Image #1). Here you can see the schema changes that were made when AD was first installed. I’ve highlighted in red the whenChanged property in its original long-date format.

In the second image (Image #2), I have scrolled down the output. We see that on the next day, I installed Exchange Server, which modifies the AD schema. It’s grouped by the ‘Event’ property, which is a calculated property. You can see that the date format is transformed into short form.

Image #2 Expand
How to Use PowerShell Calculated Properties (Image Credit: Russell Smith)


Calculated properties provide a useful way to transform data or add data to PowerShell objects so that you can get exactly the results you need. It often happens that data returned by PowerShell cmdlets isn’t in exactly the format you need. And calculated properties give you an easy way to solve that problem.

Related Topics:


Don't have a login but want to join the conversation? Sign up for a Petri Account

Comments (0)

Leave a Reply

IT consultant, Contributing Editor @PetriFeed, and trainer @Pluralsight. All about Microsoft, Office 365, Azure, and Windows Server.
External Sharing and Guest User Access in Microsoft 365 and Teams

This eBook will dive into policy considerations you need to make when creating and managing guest user access to your Teams network, as well as the different layers of guest access and the common challenges that accompany a more complicated Microsoft 365 infrastructure.

You will learn:

  • Who should be allowed to be invited as a guest?
  • What type of guests should be able to access files in SharePoint and OneDrive?
  • How should guests be offboarded?
  • How should you determine who has access to sensitive information in your environment?

Sponsored by:

Live Webinar: Active Directory Security: What Needs Immediate Priority!Live on Tuesday, October 12th at 1 PM ET

Attacks on Active Directory are at an all-time high. Companies that are not taking heed are being punished, both monetarily and with loss of production.

In this webinar, you will learn:

  • How to prioritize vulnerability management
  • What attackers are leveraging to breach organizations
  • Where Active Directory security needs immediate attention
  • Overall strategy to secure your environment and keep it secured

Sponsored by: