PowerShell Objects
last modified February 20, 2025
In this article, we will explore objects in PowerShell.
PowerShell is an object-oriented scripting language. Everything in PowerShell is an object, including variables, strings, and system components.
Creating Custom Objects
You can create custom objects using New-Object
or
[PSCustomObject]
.
custom_object.ps1
$obj = New-Object PSObject -Property @{ Name="Alice"; Age=30 } Write-Output $obj
Getting Object Properties
You can access object properties using dot notation.
get_properties.ps1
$process = Get-Process | Select-Object -First 1 Write-Output $process.Name
Modifying Object Properties
You can modify object properties after creation.
modify_object.ps1
$obj = [PSCustomObject]@{ Name="Bob"; Age=25 } $obj.Age = 26 Write-Output $obj
Filtering Objects
You can filter objects using Where-Object
.
filter_objects.ps1
$processes = Get-Process | Where-Object { $_.CPU -gt 10 } Write-Output $processes
Sorting Objects
You can sort objects using Sort-Object
.
sort_objects.ps1
$processes = Get-Process | Sort-Object -Property CPU -Descending Write-Output $processes
Selecting Object Properties
You can select specific properties using Select-Object
.
select_properties.ps1
$processes = Get-Process | Select-Object Name, CPU Write-Output $processes
Source
In this article, we have explored objects in PowerShell.
Author
List all PowerShell tutorials.