Understanding Cmdlets
Overview:
The following section goes over what cmdlets are and common cmdlets utilized within Powershell.
Common Cmdlets
What are Cmdlets?
Cmdlets are lightweight commands used in the PowerShell environment. They follow a Verb-Noun naming pattern.
Common Cmdlets:
Get-Help: Provides help information for cmdlets.Get-Help Get-ProcessGet-Command: Lists all available cmdlets and functions.Get-CommandGet-Service: Retrieves the status of services on a system.Get-ServiceGet-Process: Gets the processes that are running on a system.Get-ProcessSet-Variable: Sets the value of a variable.Set-Variable -Name "testVar" -Value "Hello, PowerShell"Remove-Item: Deletes files, directories, or registry keys.Remove-Item -Path "C:\Temp\test.txt"
Examples Using Cmdlets:
Retrieve a list of all services:
Get-ServiceGet detailed help for a cmdlet:
Get-Help Get-Process -DetailedList all commands available in your PowerShell session:
Get-Command
Using the Pipeline
What is a Pipeline?
The pipeline (
|) is a powerful feature in PowerShell that allows you to pass the output of one cmdlet as input to another cmdlet.
Basic Pipeline Usage:
Example:
Get-Process | Sort-Object -Property CPU
Filtering Output:
Use
Where-Objectto filter objects based on property values.Example:
Get-Process | Where-Object { $_.CPU -gt 100 }
Selecting Properties:
Use
Select-Objectto select specific properties of objects.Example:
Get-Process | Select-Object -Property Name, CPU
Sorting Output:
Use
Sort-Objectto sort objects by property values.Example:
Get-Process | Sort-Object -Property CPU -Descending
Example: Using Multiple Cmdlets in a Pipeline
Get-Service | Where-Object { $_.Status -eq "Running" } | Sort-Object -Property DisplayNameFiltering and Formatting Output
Filtering Data:
Where-Object: Filters objects based on a specified condition.Get-Process | Where-Object { $_.CPU -gt 100 }
Formatting Data:
Format-Table: Formats output as a table.Get-Process | Format-Table -Property Name, CPU -AutoSizeFormat-List: Formats output as a list.Get-Process | Format-List -Property Name, CPU
Examples:
List all running processes using a table format:
Get-Process | Format-Table -Property Name, CPU -AutoSizeList all services that are running, sorted by display name:
Get-Service | Where-Object { $_.Status -eq "Running" } | Sort-Object -Property DisplayName
Practical Exercises
Exercise 1: Using Basic Cmdlets
Retrieve a list of all processes running on your system:
Get-ProcessRetrieve detailed help for the
Get-Servicecmdlet:Get-Help Get-Service -Detailed
Exercise 2: Using the Pipeline
List all running processes and sort them by CPU usage:
Get-Process | Sort-Object -Property CPUFilter the processes to show only those using more than 100 units of CPU:
Get-Process | Where-Object { $_.CPU -gt 100 }
Exercise 3: Filtering and Formatting Output
List all running services and format the output as a table:
Get-Service | Where-Object { $_.Status -eq "Running" } | Format-Table -Property DisplayName, Status -AutoSize
Exercise 4: Combining Cmdlets in a Pipeline
Combine
Get-Service,Where-Object, andFormat-Tableto list all running services, sorted by their display names:Get-Service | Where-Object { $_.Status -eq "Running" } | Sort-Object -Property DisplayName | Format-Table -Property DisplayName, Status -AutoSize
Last updated