BudiBadu Logo
Samplebadu

PowerShell by Example: Cmdlets

PowerShell 7

Understanding PowerShell commands with this sample code demonstrating Verb-Noun naming convention, common cmdlets for system management, parameter usage with named arguments, and object output from cmdlet execution.

Code

# Get-Service cmdlet (Get verb, Service noun)
Get-Service -Name "wuauserv"

# Get-Process with filtering
Get-Process | Where-Object { $_.CPU -gt 100 }

# Stop-Service with WhatIf parameter
Stop-Service -Name "Spooler" -WhatIf

# Set-Location (alias: cd)
Set-Location -Path "C:\Windows"

# Get-Command to discover cmdlets
Get-Command -Verb Get -Noun Service

# Get-Help for documentation
Get-Help Get-Service -Examples

Explanation

Cmdlets are lightweight native PowerShell commands that follow a consistent Verb-Noun naming convention where the verb describes the action and the noun identifies the resource. Cmdlets are implemented as .NET classes rather than standalone executables, making them instances of .NET Framework classes that return .NET objects to the pipeline. This object-oriented design distinguishes PowerShell from traditional text-based shells.

PowerShell cmdlet verbs are standardized and include:

  • Get retrieves data or resources without modifying them
  • Set modifies or establishes data and resource configurations
  • New creates new resources or objects
  • Remove deletes resources or objects
  • Start and Stop control service and process states
  • Invoke executes commands or operations

Cmdlets support parameters that modify their behavior, specified using a dash followed by the parameter name like -Name or -Path. Common parameters available on all cmdlets with [CmdletBinding()] include -Verbose, -Debug, -ErrorAction, -WarningAction, and -WhatIf for simulation. The Get-Command cmdlet discovers available cmdlets by filtering on verbs, nouns, or modules, while Get-Help provides comprehensive documentation with examples and parameter descriptions.

Code Breakdown

2
Get-Service follows Verb-Noun convention, returns service objects.
5
Where-Object filters pipeline objects using script block conditions.
8
-WhatIf common parameter simulates command without executing.
14
Get-Command -Verb Get discovers cmdlets by filtering on verb.