• Skip to main content
  • Skip to secondary menu
  • Skip to primary sidebar
  • Skip to footer
WebSetNet

WebSetNet

Technology News

  • Technology News
    • Mobile
    • Games
  • Internet Marketing
  • System Admin
    • Windows 11
    • Linux
    • Mac & Apple
    • Website Scripts
      • Wordpress

How to Use PowerShell Calculated Properties

December 4, 2020 by Martin6

 

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

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.

The post How to Use PowerShell Calculated Properties appeared first on Petri.

Related posts:

  1. The Beginner's Guide to Structured Data for Organizing & Optimizing Your Website
  2. PowerShell Core 6.0 and Why Windows PowerShell Is No Longer Being Developed
  3. SEO for Real Estate Agents: Real Estate SEO Tips You Need to Know
  4. Fix DCOM Event ID 10016 error on Windows 10
  5. Using Windows PowerShell Modules in PowerShell 7
  6. Google 3D objects: How to view dinosaurs in AR right in your kitchen
  7. How to optimize your local business for voice search
  8. A new Minecraft: Bedrock Edition patch update is rolling out to all players
  9. How to use schema markup to make an impression in search
  10. Best Structured Data Testing Tools

Filed Under: Windows 11 Tagged With: calculated, PowerShell, properties

Primary Sidebar

Trending

  • 5 Ways to Fix “Your SIM sent a Text Message” Issue on iPhone
  • 3 Ways to Disable GetApps on Xiaomi, Redmi, and Poco Phones Running MIUI
  • How To Extract & Install tar.gz Files In Ubuntu
  • 8 Best Sites to Read Manga Online for Free
  • How to find a lost Apple Pencil using your iPad (1st and 2nd gen)
  • How to Highlight Duplicates in Google Sheets
  • Discord Stream Has No Sound? 6 Ways to Fix
  • GeForce Experience not finding games? Fix it fast
  • Exclamation Mark on Network Signal, Mobile Data Not Working? 8 Ways to Fix
  • How to Track a Stolen or Lost Nintendo Switch
  • How to Fix YouTube Server Connection Error [400] on Android
  • How To Calculate CAGR in Excel
  • How to check if your Android device supports Widevine DRM
  • What is Android System Intelligence, and why is it on your phone?
  • Troubleshooting “E: Unable to locate package” Error on Ubuntu [Beginner’s Tutorial]
  • 17 Cool Arduino Project Ideas for DIY Enthusiasts
  • How to Make Any Wired Printer Wireless in 6 Different Ways
  • 5 Best Sites To Play Scrabble Online With Friends

Footer

Tags

Amazon android Apple Asus available download: edge feature features first free from galaxy Game games gaming gets google install Intel iPhone launches linux Microsoft more OnePlus phone release released review: samsung series support this Ubuntu update using video watch what will windows with xbox your

Archives

  • September 2023
  • August 2023
  • July 2023
  • June 2023
  • May 2023
  • April 2023
  • March 2023
  • February 2023
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022
  • August 2022
  • July 2022
  • June 2022
  • May 2022
  • April 2022
  • March 2022
  • February 2022
  • January 2022
  • September 2021
  • August 2021
  • July 2021
  • June 2021
  • May 2021
  • April 2021
  • March 2021
  • February 2021
  • January 2021
  • December 2020
  • November 2020
  • October 2020
  • September 2020
  • August 2020
  • July 2020

Meta

  • Log in
  • Entries feed
  • Comments feed
  • WordPress.org