PowerShell Cmdlets
Cmdlets (pronounced command-lets) are the core of PowerShell. They are lightweight .NET classes designed for specific tasks, and they are the building blocks for all PowerShell commands. Unlike traditional command-line tools, cmdlets are designed to work together seamlessly in a pipeline.
Understanding Cmdlet Structure
Cmdlets follow a consistent verb-noun naming convention. This makes them easy to discover and understand. For example:
Get-Process
: Retrieves a list of running processes.Stop-Process
: Terminates one or more running processes.New-Item
: Creates a new item (e.g., a file or directory).Remove-Item
: Deletes an item.
The verb indicates the action to be performed, and the noun specifies the object of that action.
Common Cmdlet Categories
Cmdlets are organized into categories based on their functionality:
- Management: For managing system resources (e.g., services, event logs, registry).
- Information: For retrieving information about the system and its components.
- System: For interacting with the operating system.
- Object Manipulation: For working with PowerShell objects.
Discovering Cmdlets
You can discover cmdlets directly within PowerShell using the following commands:
Example: Finding Cmdlets
# Get all cmdlets with 'Service' in their name
Get-Command *Service*
# Get all cmdlets that can 'Get' information
Get-Command -Verb Get
# Get help for a specific cmdlet
Get-Help Get-Service
Cmdlet Parameters
Cmdlets often accept parameters to refine their actions. Parameters can be positional (their order matters) or named (you specify the parameter name). Most cmdlets support the common parameters like -Verbose
, -Debug
, -ErrorAction
, and -WhatIf
.
Example: Using Parameters
# Get details about a specific service
Get-Service -Name 'BITS'
# Stop a service and ask for confirmation
Stop-Service -Name 'Print Spooler' -Confirm
# See what would happen if you removed a directory without actually removing it
Remove-Item -Path 'C:\Temp\OldFiles' -WhatIf
Important Note on -WhatIf and -Confirm
Always use -WhatIf
before executing commands that modify or delete system resources. This parameter shows you what the command would do without making any actual changes. The -Confirm
parameter prompts you before executing the command, providing an extra layer of safety.
Pipelines
The true power of cmdlets lies in their ability to be chained together using the pipeline operator (|
). The output of one cmdlet becomes the input for the next, allowing for complex operations to be performed with concise commands.
Example: Using Pipelines
# Get all running processes, sort them by CPU usage, and display the top 5
Get-Process | Sort-Object CPU -Descending | Select-Object -First 5 -Property Name, CPU, WorkingSet
# Get all services that are not running, and then start them
Get-Service | Where-Object {$_.Status -ne 'Running'} | Start-Service
Cmdlet Binding
Cmdlet authors can define advanced features like parameter validation, attribute-based help messages, and support for common parameters through attributes in their C# code. These are managed by the PowerShell runtime.