Skip to content

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., lsGet-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

# See all approved verbs
Get-Verb

# Get specific verbs
Get-Verb -Verb Get
Get-Verb -Group Security

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

# BAD: Hard to read, not clear
ls | ? {$_.Length -gt 1MB}

# GOOD: Clear and explicit
Get-ChildItem | Where-Object {$_.Length -gt 1MB}
Aliases are great for interactive use, but scripts should use full cmdlet names for readability.

Tips & Tricks

Discover Parameters

# See all parameters for a cmdlet
Get-Help Get-Process -Parameter *

# Or use IntelliSense
Get-Process -[Ctrl+Space]  # Shows all parameters

Quick Property Access

# Get just one property
(Get-Process -Name "powershell").Name

# Or use Select-Object -ExpandProperty
Get-Process -Name "powershell" | Select-Object -ExpandProperty Name

Splatting for Readability

# Instead of long command line
Get-ChildItem -Path C:\Temp -Filter "*.log" -Recurse -ErrorAction SilentlyContinue

# Use splatting
$params = @{
    Path = "C:\Temp"
    Filter = "*.log"
    Recurse = $true
    ErrorAction = "SilentlyContinue"
}
Get-ChildItem @params

Case Insensitive

# All of these work the same
Get-Process
get-process
GET-PROCESS
gEt-PrOcEsS

# PowerShell is case-insensitive
# But use proper casing for readability

Parameter Abbreviation

# You can abbreviate parameter names
Get-Process -N "chrome"        # -N instead of -Name
Get-ChildItem -P C:\Temp       # -P instead of -Path

# But don't do this in scripts - use full names
Get-Process -Name "chrome"     # Clear and explicit

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

Additional Resources