Cmdlets Introduction
Summary
Learn what cmdlets are, how they're structured, and the essential cmdlets every PowerShell user should know.
What is a Cmdlet?
A cmdlet (pronounced "command-let") is a PowerShell command that performs a single action. Cmdlets are the building blocks of PowerShell scripts and automation.
# Examples of cmdlets
Get-Process # Gets running processes
Set-Location # Changes current directory
New-Item # Creates new item (file, folder, etc.)
Remove-Item # Deletes item
Cmdlets vs Commands
- Cmdlet: PowerShell-specific command (compiled .NET code)
- Function: User-defined command written in PowerShell
- Alias: Shortcut name for a cmdlet (e.g.,
ls→Get-ChildItem) - External Command: exe files, batch files, etc.
Verb-Noun Naming Convention
All cmdlets follow the pattern: Verb-Noun
Get-Process # Verb: Get, Noun: Process
Set-Location # Verb: Set, Noun: Location
New-Item # Verb: New, Noun: Item
Remove-Service # Verb: Remove, Noun: Service
Common Verbs
Get- # Retrieve data/information
Set- # Modify/change something
New- # Create something new
Remove- # Delete something
Start- # Start a process/service
Stop- # Stop a process/service
Restart- # Restart something
Test- # Test a condition
Invoke- # Execute/run something
Clear- # Clear/reset something
Add- # Add something to a collection
Export- # Export data
Import- # Import data
Out- # Output data to destination
Check Approved Verbs
Why Verb-Noun Matters
Predictability: Once you learn the pattern, you can guess cmdlet names:
# Want to get services? Try:
Get-Service
# Want to get processes?
Get-Process
# Want to stop a service?
Stop-Service
# Want to create a new file?
New-Item
Parameters
Parameters customize how a cmdlet works. They follow the cmdlet name with a - prefix:
# Cmdlet with parameters
Get-Process -Name "powershell"
Get-ChildItem -Path C:\Temp -Filter "*.txt"
Set-Content -Path "file.txt" -Value "Hello World"
Types of Parameters
Named Parameters
# Most common - explicitly name the parameter
Get-Process -Name "chrome"
Get-Service -Name "Spooler"
Get-ChildItem -Path C:\Temp -Filter "*.log"
Positional Parameters
# Some parameters don't need names if in correct order
Get-Process "chrome" # -Name is position 0
Get-ChildItem C:\Temp "*.log" # -Path is position 0, -Filter is position 1
# But named is clearer:
Get-ChildItem -Path C:\Temp -Filter "*.log"
Switch Parameters
# Boolean flags - just include or don't
Get-ChildItem -Recurse # Include switch = $true
Get-ChildItem # Omit switch = $false
# Explicitly set (rare)
Get-ChildItem -Recurse:$true # On
Get-ChildItem -Recurse:$false # Off
Optional vs Mandatory Parameters
# Some parameters are required
New-Item -Path "file.txt" -ItemType File # Both required
# Some are optional
Get-Process # Works without parameters
Get-Process -Name "chrome" # Optional -Name parameter
Common Parameters
All cmdlets (with [CmdletBinding()]) support these:
-Verbose # Show detailed information
-Debug # Show debugging information
-ErrorAction # How to handle errors (Stop, Continue, SilentlyContinue, Inquire)
-WarningAction # How to handle warnings
-WhatIf # Show what would happen (doesn't execute)
-Confirm # Ask for confirmation before executing
Examples:
# See verbose output
Get-Process -Name "powershell" -Verbose
# Suppress errors
Get-Item "C:\DoesNotExist.txt" -ErrorAction SilentlyContinue
# Test what would happen without doing it
Remove-Item C:\Temp\*.log -WhatIf
# Ask for confirmation
Remove-Item C:\Important -Recurse -Confirm
Tab Completion
PowerShell provides intelligent tab completion:
# Complete cmdlet names
Get-Pro[Tab] # Cycles: Get-Process, Get-ProvisioningPackage...
# Complete parameters
Get-Process -N[Tab] # Expands to: Get-Process -Name
# Complete parameter values
Get-Service -Name [Tab] # Cycles through actual service names
# Complete file paths
Get-ChildItem C:\Pro[Tab] # Expands to: C:\Program Files\
# Show all options
Get-Process [Ctrl+Space] # Shows ALL available parameters
Essential Cmdlets Everyone Should Know
File System Navigation
# Get current location
Get-Location # Shows current directory
pwd # Alias
# Change location
Set-Location C:\Temp # Change to directory
cd C:\Temp # Alias
cd .. # Go up one level
# List items
Get-ChildItem # List files/folders
ls # Alias
dir # Alias
gci # Alias
# With parameters
Get-ChildItem -Path C:\Temp -Filter "*.txt" -Recurse
File Operations
# Create item
New-Item -Path "test.txt" -ItemType File
New-Item -Path "MyFolder" -ItemType Directory
# Copy item
Copy-Item -Path "file.txt" -Destination "backup.txt"
Copy-Item -Path "C:\Folder" -Destination "D:\Backup" -Recurse
# Move item
Move-Item -Path "file.txt" -Destination "C:\NewLocation\"
# Remove item
Remove-Item -Path "file.txt"
Remove-Item -Path "C:\OldFolder" -Recurse -Force
# Get item details
Get-Item -Path "file.txt"
Get-ItemProperty -Path "file.txt"
Reading and Writing Files
# Read file
Get-Content -Path "file.txt"
cat file.txt # Alias
# Write to file (overwrites)
Set-Content -Path "file.txt" -Value "New content"
"New content" | Set-Content -Path "file.txt"
# Append to file
Add-Content -Path "file.txt" -Value "Additional line"
"Additional line" | Add-Content -Path "file.txt"
# Write to file (alternative)
"Some text" | Out-File -FilePath "output.txt"
Process Management
# Get processes
Get-Process # All processes
Get-Process -Name "chrome" # Specific process
Get-Process | Where-Object {$_.CPU -gt 100} # Filter by CPU
# Stop process
Stop-Process -Name "notepad"
Stop-Process -Id 1234
Get-Process chrome | Stop-Process # Pipeline
Service Management
# Get services
Get-Service # All services
Get-Service -Name "Spooler" # Specific service
Get-Service | Where-Object {$_.Status -eq "Running"}
# Start/Stop services
Start-Service -Name "Spooler"
Stop-Service -Name "Spooler"
Restart-Service -Name "Spooler"
# Get service status
(Get-Service -Name "Spooler").Status
System Information
# Get computer info
Get-ComputerInfo
hostname # Computer name
$env:COMPUTERNAME # Computer name variable
# Get environment variables
Get-ChildItem Env: # All environment variables
$env:USERNAME # Current user
$env:PATH # PATH variable
# Get date/time
Get-Date
Get-Date -Format "yyyy-MM-dd"
Finding Commands
# Find cmdlets
Get-Command # All commands
Get-Command -Name "*Process*" # Commands with "Process" in name
Get-Command -Verb Get # All Get- cmdlets
Get-Command -Noun Service # All *-Service cmdlets
# Find by module
Get-Command -Module ActiveDirectory
Understanding Cmdlet Output
Cmdlets return objects with properties and methods:
# Get a process
$proc = Get-Process -Name "powershell" | Select-Object -First 1
# Access properties
$proc.Name # Property: Name
$proc.CPU # Property: CPU usage
$proc.WorkingSet # Property: Memory usage
# See all properties
$proc | Get-Member -MemberType Property
# See all methods
$proc | Get-Member -MemberType Method
# Call methods
$proc.Kill() # Method: Kill the process
Common Cmdlet Patterns
Pattern 1: Get → Filter → Format
Get-Process |
Where-Object {$_.CPU -gt 50} |
Sort-Object -Property CPU -Descending |
Format-Table Name, CPU, WS
Pattern 2: Get → Modify → Set
# Get content, modify, save back
$content = Get-Content -Path "config.txt"
$content = $content -replace "old", "new"
Set-Content -Path "config.txt" -Value $content
Pattern 3: Get → Export
# Export to different formats
Get-Process | Export-Csv -Path "processes.csv"
Get-Service | ConvertTo-Json | Out-File "services.json"
Get-Process | Export-Clixml -Path "processes.xml"
Aliases
PowerShell provides familiar aliases for common cmdlets:
# Common aliases
ls → Get-ChildItem
dir → Get-ChildItem
cd → Set-Location
pwd → Get-Location
cat → Get-Content
clear → Clear-Host
cls → Clear-Host
copy → Copy-Item
move → Move-Item
del → Remove-Item
rm → Remove-Item
# See all aliases
Get-Alias
# Find what an alias does
Get-Alias ls
# Find aliases for a cmdlet
Get-Alias -Definition Get-ChildItem
# Create your own alias
Set-Alias -Name np -Value notepad
Don't Use Aliases in Scripts
Aliases are great for interactive use, but scripts should use full cmdlet names for readability.Tips & Tricks
Discover Parameters
Quick Property Access
Splatting for Readability
Case Insensitive
Parameter Abbreviation
Common Beginner Mistakes
Mistake 1: Not Using Tab Completion
# Hard way:
Get-ChildItem -Path C:\Users\YourName\Documents\Projects\MyProject
# Easy way:
Get-ChildItem -Path C:\U[Tab]\Y[Tab]\D[Tab]\P[Tab]\M[Tab]
Mistake 2: Forgetting -Recurse
# Only gets files in current folder
Get-ChildItem -Path C:\Logs -Filter "*.log"
# Gets files in all subfolders too
Get-ChildItem -Path C:\Logs -Filter "*.log" -Recurse
Mistake 3: Using Wrong Parameter Type
# Wrong: -Name expects string, not number
Get-Process -Name 1234
# Right: Use -Id for process ID
Get-Process -Id 1234
Related Topics
- Help System - Learning how to use cmdlets
- The Pipeline - Chaining cmdlets together
- Functions - Creating your own cmdlets
- Variables & Data Types - Storing cmdlet output