Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

PowerShell Object-Oriented Command Line

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 management more efficient.

What is an 'object-oriented command line'

In traditional command lines, commands pass string text through the pipe |; to process this text further, you usually need tools like grep, awk, and sed.

In PowerShell, commands pass objects through the pipeline, and you can directly access the objects' properties and methods without parsing text, greatly improving accuracy and development efficiency.

Commands output objects

Take the Get-Process command as an example:



Get-Process

This command is used to get the list of processes running in the current system.

The Get-Process command does not return a plain text table but a set of .NET objects, where each process is an object of type System.Diagnostics.Process.

We can access its properties just as we access objects in a programming language; for example:



(Get-Process)[0].Name

The command above gets the name of the first process; example output:



explorer


Inspect an object's structure: Get-Member

To deeply understand an object, the key is to know what properties and methods it has. PowerShell provides Get-Member command to help us explore object structure:

Get-Process | Get-Member

Partial output example:



Name        MemberType   Definition

----        ----------   ----------

Id          Property     int Id {get;}

Name        Property     string Name {get;}

Kill        Method       void Kill()

Start       Method       void Start()

Explanation:

You can use 对象.属性 or 对象.方法() Use these members in the form of dot notation.


Object pipeline passing

The pipeline passes objects

In PowerShell, multiple commands connect through | and what is passed isobjectrather than text.
This means you can directly access the object properties passed by the previous command in subsequent commands.

Example: Filter all services whose status is Running :

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

Explanation:

Pipeline operation flowchart (Mermaid)


Common object-processing commands

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

Command Description
Where-Object Filter objects by condition
Select-Object Select or rename object properties
Sort-Object Sort objects by a certain property
Group-Object Group and count objects by property
Measure-Object Count objects, sum, average, etc.
Export-Csv Export objects to a CSV file
ConvertTo-Json Convert objects to a JSON format string

Object property filtering and calculated columns

Select specific properties

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

This command extracts each process's name, ID, and CPU usage.

Add a calculated column (custom property)

You can add a custom property via a script block:

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

Explanation:


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, ...}


Exporting 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 facilitate integration with other systems or APIs and are an important part of automation workflows.


Typical practical cases

Case 1: Find the top 5 CPU-consuming processes

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 nature makes command-line operations more aligned with programming thinking. Through object properties and methods, users can manipulate system resources more precisely and build more powerful and reliable automation scripts.

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

  1. Use more often Get-Member to explore object structure
  2. Become proficient with Where-Object and Select-Object usage
  3. Try using Group-Object, Sort-Object to process data
  4. Try exporting command results to CSV or JSON files