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
- Simple Objects: Strings, integers, and booleans.
- Complex Objects: Arrays, hash tables, and custom objects.
- 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 = $trueThese 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 $firstStateHere is the exact output in the screenshot below:

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 $firstNameHere is the exact output in the screenshot below:

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.LastNameRead 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:
Bijay Kumar is an esteemed author and the mind behind PowerShellFAQs.com, where he shares his extensive knowledge and expertise in PowerShell, with a particular focus on SharePoint projects. Recognized for his contributions to the tech community, Bijay has been honored with the prestigious Microsoft MVP award. With over 15 years of experience in the software industry, he has a rich professional background, having worked with industry giants such as HP and TCS. His insights and guidance have made him a respected figure in the world of software development and administration. Read more.