How to Create Objects in PowerShell?

As a PowerShell developer, you should know how to create and manipulate objects. In this tutorial, I will explain how to create objects in PowerShell with detailed examples.

What Are Objects in PowerShell

Objects allow you to store, manipulate, and retrieve data efficiently in PowerShell. In PowerShell, objects can represent anything from a simple string or number to complex data structures like files, directories, or even system processes.

Custom objects are helpful when dealing with large datasets or when you need to pass data between different parts of your PowerShell script.

Types of Objects in PowerShell

  1. Simple Objects: Strings, integers, and booleans.
  2. Complex Objects: Arrays, hash tables, and custom objects.
  3. System Objects: Files, directories, processes, and more.

Check out PowerShell Sort-Object

Create Simple Objects in PowerShell

Let’s start with simple objects. These are the basic data types like strings, integers, and booleans.

# Creating a string object
$stringObject = "Hello, PowerShell!"

# Creating an integer object
$integerObject = 42

# Creating a boolean object
$booleanObject = $true

These simple objects can be used directly in your scripts to perform various tasks.

Read PowerShell Select-Object -First

Create Complex Objects in PowerShell

In PowerShell, complex objects include arrays and hash tables, which are collections of simple objects.

Let me show you how to create those complex objects in PowerShell.

Arrays

Arrays are ordered collections of objects. You can create an array by enclosing a comma-separated list of values in @().

# Creating an array of strings
$statesArray = @("California", "Texas", "New York", "Florida")

# Accessing elements in the array
$firstState = $statesArray[0]
Write-Host $firstState

Here is the exact output in the screenshot below:

Create Complex Objects in PowerShell

Hash Tables

Hash tables are collections of key-value pairs. They are useful for storing related data.

# Creating a hash table
$personHashTable = @{
    Name = "John Doe"
    Age = 30
    State = "California"
}

# Accessing values in the hash table
$name = $personHashTable["Name"]

Check out PowerShell Select-Object

Create Custom Objects in PowerShell

Custom objects are user-defined objects that allow you to group related data together. You can create custom objects using the New-Object cmdlet or the [PSCustomObject] type accelerator.

Using New-Object

The New-Object cmdlet is a traditional way to create custom objects in PowerShell. Here is the complete PowerShell script.

# Creating a custom object using New-Object
$customObject = New-Object -TypeName PSObject -Property @{
    FirstName = "Jane"
    LastName = "Smith"
    City = "Seattle"
    State = "Washington"
}

# Accessing properties of the custom object
$firstName = $customObject.FirstName
Write-Host $firstName

Here is the exact output in the screenshot below:

Create Objects in PowerShell

Using [PSCustomObject]

The [PSCustomObject] type accelerator is a more modern and preferred way to create custom objects in PowerShell.

Here is the complete script:

# Creating a custom object using [PSCustomObject]
$customObject = [PSCustomObject]@{
    FirstName = "Michael"
    LastName = "Johnson"
    City = "Austin"
    State = "Texas"
}

# Accessing properties of the custom object
$lastName = $customObject.LastName

Read How to Convert String to Object in PowerShell?

PowerShell Custom Object Example: Managing Employee Data

Let’s consider a real-world scenario where you need to manage employee data for a company. You want to create a custom object for each employee and store it in an array.

Here is the complete PowerShell script.

# Creating a list of employees
$employees = @()

# Adding employees to the list
$employees += [PSCustomObject]@{
    FirstName = "Alice"
    LastName = "Brown"
    City = "San Francisco"
    State = "California"
    Position = "Software Engineer"
}

$employees += [PSCustomObject]@{
    FirstName = "Bob"
    LastName = "Davis"
    City = "New York"
    State = "New York"
    Position = "Project Manager"
}

# Displaying employee data
foreach ($employee in $employees) {
    Write-Output "Name: $($employee.FirstName) $($employee.LastName), City: $($employee.City), State: $($employee.State), Position: $($employee.Position)"
}

This script creates an array of custom objects, each representing an employee. You can easily add, remove, or modify employee data by manipulating the array.

Read Convert Object to String in PowerShell

Advanced PowerShell Custom Object Features

PowerShell custom objects offer advanced features like methods and calculated properties.

Let me show you two examples.

Adding Methods to Custom Objects

You can add methods to custom objects to define custom behavior.

# Creating a custom object with a method
$employee = [PSCustomObject]@{
    FirstName = "Charlie"
    LastName = "Evans"
    City = "Chicago"
    State = "Illinois"
}

# Adding a method to the custom object
$employee | Add-Member -MemberType ScriptMethod -Name "GetFullName" -Value {
    return "$($this.FirstName) $($this.LastName)"
}

# Calling the method
$fullName = $employee.GetFullName()
Write-Output "Full Name: $fullName"

Calculated Properties

Calculated properties allow you to define properties based on existing data.

# Creating a custom object with calculated properties
$employee = [PSCustomObject]@{
    FirstName = "David"
    LastName = "Wilson"
    City = "Houston"
    State = "Texas"
}

# Adding a calculated property
$employee | Add-Member -MemberType NoteProperty -Name "FullName" -Value {
    "$($this.FirstName) $($this.LastName)"
}

# Accessing the calculated property
$fullName = $employee.FullName
Write-Output "Full Name: $fullName"

Conclusion

In this tutorial, I explained how to create objects in PowerShell to manage and manipulate data. Here, we learned how to work with simple data types, complex collections, custom objects, etc.

You may also like:

100 PowerShell cmdlets download free

100 POWERSHELL CMDLETS E-BOOK

FREE Download an eBook that contains 100 PowerShell cmdlets with complete script and examples.