Mastering Object Manipulation in PowerShell: A Guide to Essential Cmdlets

PowerShell excels at manipulating objects, the heart of most data it works with. This blog post dives into the essential object manipulation cmdlets, empowering you to filter, sort, format, group, and analyze data effectively in your scripts.

1. Select-Object: Picking the Perfect Properties

This cmdlet allows you to choose specific properties you want to display from an object. Here’s how it works:

PowerShell

Get-Process | Select-Object Name, CPU -ExpandProperty Name  # Select Name and CPU properties, then expand Name into a list

This command retrieves process information, selects only the “Name” and “CPU” properties, and then expands the “Name” property into a list of process names.

2. Sort-Object: Arranging Objects in Order

This cmdlet lets you sort objects based on specific properties. Sort order can be ascending or descending.

PowerShell

Get-Service | Sort-Object Status  # Sort services by their Status property
Get-Process | Sort-Object CPU -Descending  # Sort processes by CPU usage (descending order)

The first command sorts services by their “Status” property. The second command sorts processes based on their CPU usage, with the most resource-intensive processes listed first.

3. Measure-Object: Analyzing Object Statistics

This cmdlet calculates statistical information about objects, such as count, average, minimum, and maximum values for specific properties.

PowerShell

Get-Process | Measure-Object -Property CPU -Average  # Calculate average CPU usage for all processes
Get-Service | Measure-Object -Property @{Name="RunningServices"; Expression{$_.Status -eq "Running"}}  # Count running services

The first command calculates the average CPU usage across all processes. The second command uses a calculated property to count only running services.

4. Group-Object: Categorizing Objects by Shared Values

This cmdlet groups objects based on a specific property, allowing you to analyze data by category.

PowerShell

Get-Process | Group-Object Status  # Group processes by their Status property
Get-Service | Group-Object Name -NoElement  # Group services by Name but hide individual service details

The first command groups processes based on their “Status” (Running, Stopped, etc.). The second command groups services by “Name” but hides the details of each service within the group.

5. Where-Object: Filtering Objects Based on Conditions

This cmdlet allows you to filter objects based on specific criteria. You can use comparison operators and wildcards for flexible filtering.

PowerShell

Get-Process | Where-Object {$_.Name -like "*notepad*"}  # Filter processes with "notepad" in their name
Get-Service | Where-Object {$_.Status -eq "Stopped" -and $_.Name -notlike "*sql*"}  # Filter for stopped services except those with "sql" in their name

The first command filters for processes containing “notepad” in their name. The second command filters for stopped services, excluding those with names containing “sql.”

6. ForEach-Object: Processing Objects Individually

This cmdlet allows you to iterate through each object in a collection and perform an action on each one.

PowerShell

Get-Process | ForEach-Object { Write-Host "Process Name: ${\_.Name}, CPU: ${\_.CPU}%"}  # Display details for each process
Get-Service | ForEach-Object { Stop-Service $_.Name -Force  # Stop all running services (use with caution)

The first command iterates through each process and displays its name and CPU usage. The second command (use with caution) iterates through each service and stops it forcefully.

7. New-Object: Constructing Custom Objects

This cmdlet allows you to create new objects with specific properties and values. This can be useful for storing data or building complex data structures.

PowerShell

$computerInfo = New-Object PSObject -Property @{
  "ComputerName" = "Server1"
  "OSVersion" = "Windows 10"
  "Memory" = (Get-WmiObject Win32_OperatingSystem).TotalVisibleMemorySize / 1MB
}
Write-Host $computerInfo  # Display the newly created object with its properties

This code creates a new object named $computerInfo with three properties: “ComputerName,” “OSVersion,” and “Memory.” The “Memory” property retrieves the total visible memory size from the system and converts it to megabytes.

Powershell

Leave a Reply

Your email address will not be published. Required fields are marked *

×