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:

CN=Schema,CN=Configuration,DC=ad,DC=globomantics,DC=uk

-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
Figure1 4
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
Figure2 1
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.