Working with Objects

When Objects Clicked

I was writing a script to generate a report of all users from Active Directory. Coming from text-based scripting, my first instinct was to output user information as formatted text, then parse it later. It looked messy:

# My first terrible approach
Get-ADUser -Filter * | Format-Table Name, Email, Department
# Now I needed to parse this formatted text... yikes

A senior engineer reviewed my code and shook his head. "You're thinking in text. PowerShell gives you objects. Use them."

He showed me:

# The right way
$users = Get-ADUser -Filter * -Properties *
$users | Where-Object {$_.Department -eq 'IT'} | 
    Select-Object Name, EmailAddress, Department |
    Export-Csv -Path "IT_Users.csv"

Clean. Simple. No parsing. That's when I truly understood that PowerShell objects aren't just data structures - they're the foundation of everything PowerShell does well.

Understanding PowerShell Objects

In PowerShell, almost everything is an object. When you run Get-Process, you don't get text - you get process objects. Each object has:

  • Properties: Data about the object (Name, CPU, Memory, etc.)

  • Methods: Actions the object can perform (Kill, Refresh, etc.)

  • Type: What kind of object it is (System.Diagnostics.Process)

Exploring Objects with Get-Member

The most important command for understanding objects:

Output shows:

  • TypeName: The .NET type of the object

  • Name: Property or method name

  • MemberType: Whether it's a property, method, etc.

  • Definition: Technical details about the member

Accessing Object Properties

Two ways to access properties:

Calling Object Methods

Methods are actions objects can perform:

Deep Dive: Select-Object

Select-Object is your Swiss Army knife for working with objects.

Selecting Specific Properties

Limiting Results

Calculated Properties

Create custom properties on the fly:

Calculated properties are essential for:

  • Converting units (bytes to MB/GB)

  • Formatting data (dates, numbers)

  • Creating conditional values

  • Combining multiple properties

Expanding Properties

Sometimes properties contain nested objects or arrays:

Deep Dive: Where-Object

Where-Object filters objects based on conditions.

Basic Filtering

Comparison Operators

Multiple Conditions

Simplified Syntax (PowerShell 3.0+)

Advanced Object Manipulation

Sorting Objects

Grouping Objects

Measuring Objects

Working with Complex Objects

Nested Properties

Objects often contain other objects:

Array Properties

Some properties contain arrays:

Creating Custom Objects

Sometimes you need to create your own objects:

Using PSCustomObject

Adding Properties to Existing Objects

Real-World Examples

Server Inventory Report

Creating a custom inventory from multiple sources:

Log Analysis

Processing log files into structured objects:

Resource Usage Dashboard

Combining multiple data sources:

Common Pitfalls

1. Modifying Objects in the Pipeline

Problem: Objects in the pipeline can't be modified directly Solution: Create new objects with desired properties

2. Forgetting About Property Types

Problem: Treating all properties as strings Solution: Check property types and convert as needed

3. Not Using Calculated Properties

Problem: Trying to format data outside PowerShell Solution: Use calculated properties in Select-Object

4. Overusing Select-Object *

Problem: Select-Object * returns everything, including hidden properties Solution: Only select what you need for better performance

Key Takeaways

  • Everything in PowerShell is an object with properties and methods

  • Get-Member reveals object structure - use it constantly

  • Select-Object controls which properties you work with

  • Where-Object filters objects based on conditions

  • Calculated properties transform data on the fly

  • Custom objects combine data from multiple sources

  • Object types matter - understand what you're working with

  • Don't format early - keep objects as objects until final output

What You've Learned

✅ How to explore objects with Get-Member ✅ Accessing object properties and methods ✅ Advanced Select-Object techniques including calculated properties ✅ Complex filtering with Where-Object ✅ Creating custom objects for reporting ✅ Working with nested and complex objects ✅ Real-world object manipulation patterns

Next Steps

Now that you master objects, it's time to learn how to store and manipulate data with variables. In Variables and Data Types, you'll discover:

  • Creating and using variables

  • PowerShell data types

  • Arrays and hashtables

  • Variable scope and lifetime

  • Best practices for variable naming

Variables are the foundation of reusable scripts - let's make your code flexible and maintainable.


Ready to work with variables? Continue to Variables and Data Types

Last updated