PowerShell Object-Oriented Command Line

The biggest difference between PowerShell and traditional command lines (such as CMD or Bash) is that PowerShell's command output is objects, not plain text.

Compared with traditional command lines, PowerShell is more flexible and powerful, and also makes scripting and system administration more efficient.

What is an "Object-Oriented Command Line"

In traditional command lines, what is passed between commands through pipes|is string text. To further process this text, you usually need to use tools such as grep, awk, and sed.

In PowerShell, however, what is passed between commands through the pipe are objects. You can directly access the properties and methods of objects without parsing text, greatly improving accuracy and development efficiency.

Command output is objects

withGet-ProcessTake the command as an example:

Get-Process

This command is used to obtain the list of processes currently running on the system.

What the Get-Process command returns is not a plain-text table, but a set of .NET objects; each process is an object of type System.Diagnostics.Process.

We can access its properties just like accessing objects in a programming language, for example:

(Get-Process)[0].Name

The above command gets the name of the first process. Example output:

explorer

View object structure: Get-Member

To understand objects deeply, the key is to know what properties and methods they have. PowerShell provides theGet-Membercommand to help us explore object structure:

Get-Process | Get-Member

Partial example output:

Name        MemberType   Definition
----        ----------   ----------
Id          Property     int Id {get;}
Name        Property     string Name {get;}
Kill        Method       void Kill()
Start       Method       void Start()

Explanation:

  • PropertyRepresents a property of the object, such asId、Name
  • MethodRepresents a method of the object, such asKill()、Start()

You can useObject.propertyorObject.method()to use these members.


Object pipeline passing

The pipeline passes objects

In PowerShell, when multiple commands are connected with|what is passed areobjectsrather than text.
This means you can directly access the properties of the objects passed from the previous command in subsequent commands.

Example: filter all servicesRunningwhose status is Running:

Get-Service | Where-Object { $_.Status -eq 'Running' }

Explanation:

  • $_Represents the current object in the pipeline
  • Statusis a property of the service object

Pipeline operation flowchart (Mermaid)


Common object processing commands

PowerShell provides a series of built-in commands for operating on objects, commonly used for filtering, sorting, grouping, exporting, and other scenarios:

Command Description
Where-Object Filters objects by condition
Select-Object Selects or renames object properties
Sort-Object Sorts objects by a property
Group-Object Groups and counts objects by property
Measure-Object Calculates count, sum, average, etc. of objects
Export-Csv Exports objects to a CSV file
ConvertTo-Json Converts objects to JSON format strings

Object property filtering and calculated columns

Selecting specific properties

Get-Process | Select-Object Name, Id, CPU

This command extracts the name, ID, and CPU usage of each process.

Adding calculated columns (custom properties)

You can add custom properties through script blocks:

Get-Process | Select-Object Name, @{Name="MemoryMB";Expression={$_.WorkingSet / 1MB -as [int]}}

Explanation:

  • @{}Create a hashtable to define a new property
  • NameSpecify the new column name
  • ExpressionProvide the calculation logic

Sorting and grouping operations

Sort processes by CPU usage

Get-Process | Sort-Object CPU -Descending

Group by service status

Get-Service | Group-Object Status

Example output:

Count Name                      Group
----- ----                      -----
   75 Running                   {WinDefend, WSearch, ...}
   35 Stopped                   {Fax, Spooler, ...}

Export and structured data

Export to a CSV file

Get-Service | Select-Object Name, Status | Export-Csv -Path services.csv -NoTypeInformation

Convert to JSON format

Get-Service | ConvertTo-Json

These formats make it easy to integrate with other systems or APIs, and are an important part of automated workflows.


Typical practical examples

Case 1: Find the top 5 processes consuming the most CPU

Get-Process | Sort-Object CPU -Descending | Select-Object -First 5 Name, CPU

Case 2: Count by service status

Get-Service | Group-Object Status | Select-Object Name, Count

Summary and suggestions

PowerShell's object-oriented features make command-line operations more aligned with programming thinking. Through object properties and methods, users can operate on system resources more precisely and build more powerful and reliable automation scripts.

Beginners are advised to consolidate their skills from the following aspects:

  1. Use more often:Get-Memberexplore object structure
  2. Become proficient inWhere-ObjectandSelect-Objectusage
  3. Try usingGroup-Object、Sort-Objectto process data
  4. Try exporting command results to CSV or JSON files
Other extensions