PowerShell for Beginners
PowerShell is Microsoft's powerful command-line shell and scripting language designed for system administration and automation. For penetration testers, it's essential for Windows environments, Active Directory enumeration, and post-exploitation. This lesson introduces PowerShell fundamentals.
What You'll Learn
- PowerShell vs Command Prompt (cmd)
- Cmdlets and the Verb-Noun pattern
- The PowerShell pipeline
- Objects vs text
- Get-Help and discovering commands
- Basic navigation and file operations
- PowerShell ISE and VS Code
- Running scripts and execution policy
What Is PowerShell?
PowerShell = Command-line shell + Scripting language
- Built on .NET Framework
- Object-oriented (not just text!)
- Designed for automation
- Cross-platform (PowerShell 7+)
- Essential for Windows administration
Versions:
- Windows PowerShell (v5.1) - Built into Windows
- PowerShell Core (v6+) - Cross-platform, open-source
- PowerShell 7+ - Latest, recommended
PowerShell vs Command Prompt
Command Prompt (cmd.exe)
✗ Text-based output only
✗ Limited scripting
✗ Older commands (dir, cd, copy)
✗ Less powerful
PowerShell
✓ Object-based output
✓ Full scripting language
✓ Modern cmdlets (Get-ChildItem, Set-Location, Copy-Item)
✓ Access to .NET Framework
✓ Aliases for cmd commands
Cmdlets: The Heart of PowerShell
Verb-Noun Pattern
Cmdlet = Command + Let (small command)
Format: Verb-Noun
Examples:
Get-Process # Get list of processes
Stop-Service # Stop a service
New-Item # Create new item (file/folder)
Set-Location # Change directory
Common Verbs
Get- = Retrieve information
Set- = Change/configure
New- = Create new item
Remove- = Delete item
Start- = Start process/service
Stop- = Stop process/service
Test- = Test/verify
Invoke- = Execute/run
Examples:
# Get all cmdlets
Get-Command
# Get all "Get" cmdlets
Get-Command -Verb Get
# Get all Process cmdlets
Get-Command -Noun Process
# Count cmdlets
(Get-Command).Count
Getting Started
Opening PowerShell
Windows PowerShell:
Start Menu → Windows PowerShell
Or: Win + X → Windows PowerShell
PowerShell as Admin:
Right-click → Run as Administrator
Check Version:
$PSVersionTable
Basic Navigation
Get Current Location
# PowerShell cmdlet
Get-Location
# Alias (like Linux pwd)
pwd
# Short form
gl
Change Directory
# PowerShell cmdlet
Set-Location C:\Users
# Alias (like cd)
cd C:\Users
# Go to home directory
cd ~
# Go up one level
cd ..
# Go to previous location
cd -
List Files and Folders
# PowerShell cmdlet
Get-ChildItem
# Alias
ls
dir
gci
# Show hidden files
Get-ChildItem -Force
# Recurse into subdirectories
Get-ChildItem -Recurse
# Filter by extension
Get-ChildItem *.txt
The PowerShell Pipeline
How It Works
# In cmd: Text output
dir | findstr "file"
# In PowerShell: Objects!
Get-ChildItem | Where-Object {$_.Length -gt 1MB}
Key Concept: PowerShell passes objects, not just text!
Pipeline Examples
# Get large files
Get-ChildItem | Where-Object {$_.Length -gt 1MB}
# Get running processes using over 100MB
Get-Process | Where-Object {$_.WorkingSet -gt 100MB}
# Stop a process
Get-Process notepad | Stop-Process
# Get services and filter
Get-Service | Where-Object {$_.Status -eq "Running"}
Objects vs Text
Understanding Objects
# Get a process
$proc = Get-Process | Select-Object -First 1
# View object properties
$proc | Get-Member
# Access properties
$proc.Name
$proc.Id
$proc.CPU
Object Properties and Methods
# Get properties and methods
Get-Process | Get-Member
# Properties (data about the object)
Name, Id, CPU, Memory, etc.
# Methods (actions you can perform)
Kill(), Refresh(), etc.
Getting Help
Get-Help Cmdlet
# Basic help
Get-Help Get-Process
# Detailed help
Get-Help Get-Process -Detailed
# Full help with examples
Get-Help Get-Process -Full
# Examples only
Get-Help Get-Process -Examples
# Online help (opens browser)
Get-Help Get-Process -Online
Update Help
# Update help files (run as Admin)
Update-Help
# Update for specific module
Update-Help -Module Microsoft.PowerShell.Management
Finding Commands
# Find cmdlets with "process" in name
Get-Command *process*
# Find cmdlets that work with services
Get-Command -Noun Service
# Search by verb
Get-Command -Verb Get
# Find aliases
Get-Alias
# Find what command an alias points to
Get-Alias ls
Get-Alias dir
Common Cmdlets
File and Directory Operations
# Create new file
New-Item file.txt -ItemType File
# Create new directory
New-Item MyFolder -ItemType Directory
# Copy file
Copy-Item file.txt file_backup.txt
# Move file
Move-Item file.txt C:\Destination\
# Remove file
Remove-Item file.txt
# Remove directory
Remove-Item MyFolder -Recurse
# Test if file exists
Test-Path file.txt
Working with Content
# Read file
Get-Content file.txt
# Read first 10 lines
Get-Content file.txt -Head 10
# Read last 10 lines
Get-Content file.txt -Tail 10
# Write to file (overwrite)
Set-Content file.txt "Hello, World!"
# Append to file
Add-Content file.txt "New line"
# Create file with content
"Hello" | Out-File file.txt
Process Management
# Get all processes
Get-Process
# Get specific process
Get-Process -Name chrome
# Sort by CPU usage
Get-Process | Sort-Object CPU -Descending
# Stop a process
Stop-Process -Name notepad
# Stop process by ID
Stop-Process -Id 1234
# Start a process
Start-Process notepad
Service Management
# Get all services
Get-Service
# Get running services
Get-Service | Where-Object {$_.Status -eq "Running"}
# Get specific service
Get-Service -Name wuauserv
# Start service (as Admin)
Start-Service -Name wuauserv
# Stop service (as Admin)
Stop-Service -Name wuauserv
# Restart service (as Admin)
Restart-Service -Name wuauserv
Aliases
What Are Aliases?
Alias = Shortcut name for cmdlet
Makes PowerShell feel familiar to cmd/bash users
Common Aliases
# Navigation
cd → Set-Location
pwd → Get-Location
ls → Get-ChildItem
dir → Get-ChildItem
# File operations
cp → Copy-Item
mv → Move-Item
rm → Remove-Item
cat → Get-Content
# Other
cls → Clear-Host
man → Get-Help
ps → Get-Process
kill → Stop-Process
View All Aliases
# List all aliases
Get-Alias
# Find alias for cmdlet
Get-Alias -Definition Get-ChildItem
# Find cmdlet for alias
Get-Alias ls
PowerShell ISE
What Is ISE?
ISE = Integrated Scripting Environment
- Built into Windows
- Script editor + console
- IntelliSense (auto-complete)
- Debugging tools
Opening ISE
Start Menu → Windows PowerShell ISE
Or: Run as Administrator for full access
ISE Features
- Script Pane: Write and save scripts
- Console Pane: Interactive commands
- Command Add-on: Browse cmdlets
- Syntax Highlighting: Color-coded
- IntelliSense: Auto-completion (Ctrl+Space)
- Run Script: F5 or green play button
VS Code for PowerShell
Why VS Code?
✓ Modern editor
✓ PowerShell extension
✓ Better than ISE
✓ Cross-platform
✓ Git integration
Setup
- Install VS Code: https://code.visualstudio.com/
- Install PowerShell Extension from Extensions marketplace
- Open
.ps1files to auto-activate PowerShell
Features
- IntelliSense
- Debugging
- Integrated terminal
- Git integration
- Extensions ecosystem
Execution Policy
What Is It?
Execution Policy = Security feature
Controls whether scripts can run
Not for security! Prevents accidental execution
Policy Levels
Restricted = No scripts allowed (default)
AllSigned = Only signed scripts
RemoteSigned = Local scripts OK, remote must be signed
Unrestricted = All scripts allowed
Bypass = Nothing is blocked
Check Policy
# View current policy
Get-ExecutionPolicy
# View all scopes
Get-ExecutionPolicy -List
Set Policy
# Set for current user (no Admin needed)
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser
# Set for entire machine (requires Admin)
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope LocalMachine
# Bypass for single command
powershell -ExecutionPolicy Bypass -File script.ps1
Running Scripts
Create and Run
# Create script
"Write-Host 'Hello, World!'" | Out-File hello.ps1
# Run script (from same directory)
.\hello.ps1
# Run with full path
C:\Scripts\hello.ps1
# Run from anywhere (add to PATH)
hello.ps1
Dot Sourcing
# Run script in current scope (keeps variables/functions)
. .\script.ps1
Practical Examples
Example 1: Find Large Files
# Find files larger than 100MB
Get-ChildItem -Recurse | Where-Object {$_.Length -gt 100MB} | Sort-Object Length -Descending
Example 2: List Running Services
# Get running services sorted by name
Get-Service | Where-Object {$_.Status -eq "Running"} | Sort-Object Name
Example 3: Kill All Chrome Processes
# Stop all Chrome processes
Get-Process chrome -ErrorAction SilentlyContinue | Stop-Process -Force
Example 4: Search for Text in Files
# Search for "password" in all .txt files
Get-ChildItem -Recurse -Filter *.txt | Select-String "password"
Example 5: Export to CSV
# Export process list to CSV
Get-Process | Export-Csv processes.csv -NoTypeInformation
Practical Pentesting Examples
Example 1: Enumerate Users
# Local users
Get-LocalUser
# Active Directory users (requires AD module)
Get-ADUser -Filter *
Example 2: Check Network Connections
# Active connections
Get-NetTCPConnection | Where-Object {$_.State -eq "Established"}
Example 3: List Installed Software
# Installed applications
Get-WmiObject -Class Win32_Product | Select-Object Name, Version
Example 4: Check for Open Shares
# Network shares
Get-SmbShare
Example 5: Download File from Web
# Download file
Invoke-WebRequest -Uri "http://example.com/file.txt" -OutFile "file.txt"
Keyboard Shortcuts
Tab = Auto-complete
Ctrl + Space = IntelliSense (ISE/VS Code)
Up/Down Arrow = Command history
F7 = Command history window
Ctrl + C = Cancel current command
Ctrl + L = Clear screen (or: cls)
Ctrl + R = Reverse search history
Quick Reference
Navigation:
Get-Location (pwd, gl) # Current directory
Set-Location (cd, sl) # Change directory
Get-ChildItem (ls, dir, gci) # List files
File Operations:
New-Item # Create file/folder
Copy-Item (cp, copy) # Copy
Move-Item (mv, move) # Move/rename
Remove-Item (rm, del) # Delete
Get-Content (cat, type, gc) # Read file
Help:
Get-Help <cmdlet> # Get help
Get-Command # Find cmdlets
Get-Alias # List aliases
Get-Member # Object properties/methods
Pipeline:
| # Pipe objects
Where-Object # Filter
Select-Object # Select properties
Sort-Object # Sort
Common Pitfalls
1. Forgetting .\ for Scripts
# Wrong:
script.ps1
# Right:
.\script.ps1
2. Using cmd Syntax
# cmd way (works but not PowerShell way):
dir /s
# PowerShell way:
Get-ChildItem -Recurse
3. Not Using Pipeline
# Less efficient:
$procs = Get-Process
foreach ($proc in $procs) {
if ($proc.CPU -gt 100) {
Write-Host $proc.Name
}
}
# PowerShell way:
Get-Process | Where-Object {$_.CPU -gt 100} | Select-Object Name
Resources
Built-in Help
Get-Help about_* # All "about" topics
Get-Help about_Variables # Learn about variables
Get-Help about_Operators # Operators
Get-Help about_Pipelines # Pipelines
Online Resources
- Microsoft Learn PowerShell docs: https://learn.microsoft.com/powershell/
- PowerShell Gallery: https://www.powershellgallery.com/
- Reddit: r/PowerShell
- SS64 PowerShell: https://ss64.com/ps/
Next Steps
- Practice daily - Use PowerShell instead of cmd
- Explore Get-Help for every cmdlet you use
- Learn the pipeline - chain commands together
- Set execution policy to run scripts
- Experiment in ISE or VS Code
PowerShell is the future of Windows administration and essential for modern pentesting. Master the basics now, and you'll be dangerous in Windows environments!
"PowerShell: Because clicking through GUI is so 1995."