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-Process
  • Get-Command: Lists all available cmdlets and functions.

    Get-Command
  • Get-Service: Retrieves the status of services on a system.

    Get-Service
  • Get-Process: Gets the processes that are running on a system.

    Get-Process
  • Set-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 detailed help for a cmdlet:

  • List all commands available in your PowerShell session:

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:

Filtering Output:

  • Use Where-Object to filter objects based on property values.

  • Example:

Selecting Properties:

  • Use Select-Object to select specific properties of objects.

  • Example:

Sorting Output:

  • Use Sort-Object to sort objects by property values.

  • Example:

Example: Using Multiple Cmdlets in a Pipeline

Filtering and Formatting Output

Filtering Data:

  • Where-Object: Filters objects based on a specified condition.

Formatting Data:

  • Format-Table: Formats output as a table.

  • Format-List: Formats output as a list.

Examples:

  • List all running processes using a table format:

  • List all services that are running, sorted by display name:

Practical Exercises

Exercise 1: Using Basic Cmdlets

  • Retrieve a list of all processes running on your system:

  • Retrieve detailed help for the Get-Service cmdlet:

Exercise 2: Using the Pipeline

  • List all running processes and sort them by CPU usage:

  • Filter the processes to show only those using more than 100 units of CPU:

Exercise 3: Filtering and Formatting Output

  • List all running services and format the output as a table:

Exercise 4: Combining Cmdlets in a Pipeline

  • Combine Get-Service, Where-Object, and Format-Table to list all running services, sorted by their display names:

Last updated